码迷,mamicode.com
首页 > 数据库 > 详细

[leetcode]110BalancedBinaryTree平衡二叉树

时间:2018-01-25 19:54:20      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:class   判断   return   开始   bsp   color   平衡二叉树   pos   ==   

public boolean isBalanced(TreeNode root) {
        int res = helper(root);
        if (res<0) return false;
        return true;
    }
    public int helper(TreeNode root)
    {
        if (root==null) return 0;
        //从底下开始判断是否平衡树
        //两个变量如果是-1就代表是不平衡
        int ld = helper(root.left);
        int rd = helper(root.right);
        //三种情况就不平衡:左右子树不平衡,本节点不平衡
        if (ld==-1||rd==-1||Math.abs(ld-rd)>1)
            return -1;
        else if (ld>rd) return ld+1;
        else return rd+1;
    }

 

[leetcode]110BalancedBinaryTree平衡二叉树

标签:class   判断   return   开始   bsp   color   平衡二叉树   pos   ==   

原文地址:https://www.cnblogs.com/stAr-1/p/8352809.html

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