题解 | #判断是不是平衡二叉树#

判断是不是平衡二叉树

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

class Solution {
public:
    bool result = true;
    int dfs(TreeNode* pRoot){
        if(pRoot == NULL)
            return 0;
        int left = dfs(pRoot->left);
        int right = dfs(pRoot->right);
        if(result && abs(left - right) >= 2)
            result = false;
        return max(left,right) + 1;
    }
    bool IsBalanced_Solution(TreeNode* pRoot) {
        if(pRoot == NULL)
            return true;
        dfs(pRoot);
        return result;
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务