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

class Solution {
public:
    bool IsBanlanced(TreeNode* pRoot,int* pDepth){
        if(pRoot==NULL){
            *pDepth=0;
            return true;
        }
        int nleft,nright;
        if(IsBanlanced(pRoot->left,&nleft) && 
        IsBanlanced(pRoot->right,&nright)){
            int diff=nleft-nright;
            if(diff<=1 && diff>=-1){
                *pDepth=1+(nleft>nright?nleft:nright);
                return true;
            }
        }

        return false;
    }

    bool IsBalanced_Solution(TreeNode* pRoot) {
        int depth=0;
        return IsBanlanced(pRoot,&depth);
    }
};
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/Ananbei/article/details/81607483
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞