码迷,mamicode.com
首页 > 其他好文 > 详细

如何判断是平衡二叉树

时间:2018-03-15 17:58:36      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:高度   height   color   null   roo   依据   nod   class   als   

依据:平衡二叉树的左右子树高度差不超过1

  private boolean isBalanced = true;
    
    public boolean isbalanced_solutions(TreeNode root) {
        height(root);
        return isBalanced;
    }

    private int height(TreeNode root) {
        if(null == root) {
            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);
    }

 

如何判断是平衡二叉树

标签:高度   height   color   null   roo   依据   nod   class   als   

原文地址:https://www.cnblogs.com/cherish010/p/8574688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!