平衡二叉树的判断,先序遍历,后序遍历的Java实现

平衡二叉树

http://www.nowcoder.com/questionTerminal/8b3b95850edb4115918ecebdf1b4d222

解法1:
1.计算每个节点的高度
2.从根节点开始从上往下遍历,判断每个节点的左右子树是否是平衡的
思路简单。。。
缺点:每次遍历都要重新计算高度,很多节点的高度都重复计算了

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null)
            return true;
//先判断root节点满不满足平衡条件
        if(Math.abs(TreeDepth(root.left)-TreeDepth(root.right))>1)
            return false;
//在判断左右子树满不满足平衡条件,类似于二叉树的先根遍历
        return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
    }  
    public int TreeDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
    }

}

解法2:
从根节点开始,从上往下遍历,按照中序遍历的思想,从左右子节点向根节点遍历,一依次判断平衡状态,这样根结点可以重复利用已经计算的子节点的高度,只需要依次遍历整棵树。在遇到某个子树非平衡时,能直接结束,返回false。
实现1,这个实现比较巧妙,在遍历每层时创建一个临时变量,参考了C++实现,不过用到了引用传递,但我们都知道Java中Int类型变量传递过程中是值传递,所以创建了一个封装数值的对象。
完整代码如下:

class Num{
    int val;
}
public class Solution {
    boolean IsBalanced_Solution(TreeNode  pRoot) {
        Num depth=new Num();
        return IsBalanced(pRoot,depth);
    }

    boolean IsBalanced(TreeNode  pRoot,Num pDepth){
        if(pRoot==null){
             pDepth.val=0;
            return true;
        }
        Num leftdepth=new Num(),rightdepth=new Num(); //在递归中声明临时变量,用于求父节点传来的指针对应的值。
 //先判断左右子树满不满足平衡条件,同时记录左右子树的深度
       if(IsBalanced(pRoot.left,leftdepth)&&IsBalanced(pRoot.right,rightdepth)){
//再判断根节点满不满足平衡条件,类似于二叉树遍历的后根遍历
            if(Math.abs(leftdepth.val-rightdepth.val)<=1){
                 pDepth.val=leftdepth.val>rightdepth.val?leftdepth.val+1:rightdepth.val+1;
                return true;
            }
        }
        return false;
    }
}

C++的实现如下:

class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
        int depth;
        return IsBalanced(pRoot,&depth);
    }

    bool IsBalanced(TreeNode* pRoot,int*pDepth){
        if(pRoot==NULL){
            *pDepth=0;
            return true;
        }

        int leftdepth,rightdepth; //在递归中声明临时变量,用于求父节点传来的指针对应的值。
        if(IsBalanced(pRoot->left,&leftdepth)&&IsBalanced(pRoot->right,&rightdepth)){
            if(abs(leftdepth-rightdepth)<=1){
                *pDepth=leftdepth>rightdepth?leftdepth+1:rightdepth+1;
                return true;
            }
        }
        return false;
    }
};

实现2:

public class Solution {

public boolean IsBalanced_Solution(TreeNode root) {
        return getDepth(root) != -1;
    }

    private int getDepth(TreeNode root) {
        if (root == null) return 0;
        int left = getDepth(root.left);
        if (left == -1) return -1;
        int right = getDepth(root.right);
        if (right == -1) return -1;
        return Math.abs(left - right) > 1 ? -1 : 1 + Math.max(left, right);}
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
酷酷我灵儿帅:这去不去和线不线下面说实话没啥关系
点赞 评论 收藏
分享
怎么起名字:早知道就不读书了,害得我送外卖还得扶眼镜
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务