剑指offer----平衡二叉树

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

//无脑递归
class Solution {
public:
    int Depth(TreeNode* root)
    {
        if(root==NULL)return 0;
        
        return max(Depth(root->left),Depth(root->right))+1;
    }
    bool IsBalanced_Solution(TreeNode* pRoot) {
        if(pRoot==NULL)return true;
        if(abs(Depth(pRoot->left)-Depth(pRoot->right))>1)
        {
            return false;
        }
        else
        {
            return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
        }
    }
};

猜你喜欢

转载自blog.csdn.net/xiaocongcxc/article/details/82779818