首页 > 其他 > 详细

如何判断是平衡二叉树

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

依据:平衡二叉树的左右子树高度差不超过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);
    }

 

如何判断是平衡二叉树

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

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