题解 | #平衡二叉树#

平衡二叉树

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

public class Solution {
    public static boolean flag = true;

    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null){
            flag = true;
        }
        isBalanced(root);
        return flag;
    }

    public int isBalanced(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = isBalanced(root.left);
        int right = isBalanced(root.right);
        if(left == -1 || right == -1 || Math.abs(left - right) > 1){
            flag = false;
            return -1;
        }
        return Math.max(left, right) + 1;
    }
}
全部评论

相关推荐

shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务