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

判断是不是平衡二叉树

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

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null) return true;
        if(Math.abs(getDeep(root.left) - getDeep(root.right)) > 1)
                return false;
        return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right)?true:false;
    }
    //计算当前节点的最大深度
    public static int getDeep(TreeNode root){
        if(root == null) return 0;
        return Math.max(getDeep(root.left)+1,getDeep(root.right)+1);
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务