首页 > 其他 > 详细

剑指offer-判断是否是平衡二叉树

时间:2019-02-01 19:15:22      阅读:174      评论:0      收藏:0      [点我收藏+]

 

private boolean isBalanced = true;
    public boolean IsBalanced_Solution(TreeNode root) {
        height(root);
        return isBalanced;
    }
    
    public int height(TreeNode root) {
        if(root == null || !isBalanced) return 0;
        int left = height(root.left);
        int right = height(root.right);
        if(Math.abs(left-right)>1) isBalanced = false;
        return 1+Math.max(left, right);
    }

 

剑指offer-判断是否是平衡二叉树

原文:https://www.cnblogs.com/Roni-i/p/10346633.html

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