判断一棵树是不是平衡二叉树

class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
        getDepth(pRoot);
        return isBalanced ;
    }
 //判断左右子树是不是都是平衡二叉树   
    private :
      bool isBalanced = true;
    public:
    int getDepth(TreeNode  *root)
    {
        if(root == NULL)
            return 0;
        int left = getDepth(root->left);
        int right = getDepth(root->right);
        if((left-right) >1  || (right-left) > 1)
            isBalanced = false;
        return right > left ? right+1 : left+1;
    }
};

猜你喜欢

转载自blog.csdn.net/feixi7358/article/details/82911593