首页 > 其他 > 详细

[leetcode] 110. 平衡二叉树

时间:2018-11-06 21:14:06      阅读:128      评论:0      收藏:0      [点我收藏+]

110. 平衡二叉树

实际上递归的求每一个左右子树的最大深度即可,如果差值大于1,返回一个-1的状态上去

class Solution {
    public boolean isBalanced(TreeNode root) {
        return depth(root)!=-1;
    }

    public int depth(TreeNode root) {
        if (null == root) return 0;
        int left = depth(root.left);
        int right = depth(root.right);

        if (left != -1 && right != -1 && Math.abs(left - right) <= 1) {
            return Math.max(left, right) + 1;
        } else {
            return -1;
        }
    }
}

[leetcode] 110. 平衡二叉树

原文:https://www.cnblogs.com/acbingo/p/9918017.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!