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

判断是不是平衡二叉树

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

public class Solution {
    boolean isBa = true;
    public boolean IsBalanced_Solution(TreeNode root) {
        isBa = true;
        height(root);
        return isBa;
    }
    public int height(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int le = height(root.left);
        int ri = height(root.right);
        if(Math.abs(le - ri) > 1) {
            isBa = false;
        }
        return Math.max(le, ri) + 1;
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务