题解 | #平衡二叉树#

平衡二叉树

http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222

class Solution {
public:
    int TreeDepth(TreeNode* pRoot)    //计算二叉树深度
    {
        if(pRoot == NULL)
        {
            return 0;
        }

        if(pRoot->left == NULL && pRoot->right == NULL)
        {
            return 1;
        }    

        return 1+max(TreeDepth(pRoot->left), TreeDepth(pRoot->right));
    }

    bool IsBalanced_Solution(TreeNode* pRoot) {    //判断二叉树是否为平衡二叉树
                    if(pRoot == NULL)
                        return true;
        return abs( TreeDepth(pRoot->left) - TreeDepth(pRoot->right)  ) <=1 \
            && IsBalanced_Solution(pRoot->left)&& IsBalanced_Solution(pRoot->right);
    }
};
全部评论

相关推荐

10-22 19:44
门头沟学院 Java
面了100年面试不知...:那我得去剪个头
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务