剑指offer38-平衡二叉树

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

在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树
思路:计算根节点左右子树的高度差,大于等于2不平衡。
    bool IsBalanced_Solution(TreeNode* pRoot) {
        if(pRoot==NULL) return true;//只看root的平衡性
        if(abs(TreeDepth(pRoot->left)-TreeDepth(pRoot->right))>1)
            return false;
        return true;
        
    }
    int TreeDepth(TreeNode* pRoot)
    {
        if(pRoot==NULL) return 0;
            return 1+Max(TreeDepth(pRoot->left),TreeDepth(pRoot->right));
    }
    int Max(int a,int b)
    {
        if(a>b)return a;
        return  b;
    }
原文地址:https://www.cnblogs.com/trouble-easy/p/12976857.html