剑指Offer

https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=13&tqId=11192&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。
 
 

代码

class Solution {
    bool isBalance(TreeNode* pRoot, int &h) {
        if (!pRoot) {
            h = 0;
            return true;
        }
        int lh, rh;
        if (!isBalance(pRoot->left, lh) ||
            !isBalance(pRoot->right, rh)) return false;
        
        if (lh > rh + 1 || rh > lh + 1) return false;
        h = lh > rh ? lh + 1 : rh + 1;
        
        return true;
    }
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
        int h = 0;
        return isBalance(pRoot, h);
    }
};

原文地址:https://www.cnblogs.com/charlesblc/p/8447965.html