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

[Leetcode] Balanced Binary Tree

时间:2014-05-05 12:50:49      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   color   

问题:给一个二叉树,写一个算法判断这个树是不是balanced。

Solution #1.

第一次遇到这个问题时我的解法,如下:

bubuko.com,布布扣
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        
        int depthOfLeft = getDepth(root.left, 1);
        int depthOfRight = getDepth(root.right, 1);
        
        if(Math.abs(depthOfRight-depthOfLeft) > 1){
            return false;
        }else{
            return isBalanced(root.left) && isBalanced(root.right);
        }
    }
    
    private int getDepth(TreeNode tree, int currentDepth){
        if(tree == null){
            return currentDepth;
        }
        return Math.max(getDepth(tree.left, currentDepth+1), 
                getDepth(tree.right, currentDepth+1));
    }
}
bubuko.com,布布扣

写了一个getDepth()函数,访问每个节点都要调用一次这个函数。这个Solution也通过了leetcode的验证程序,但是后来想了想,I can do better.

下面是我对此算法时间复杂度的分析,当整棵树有N个节点时,时间复杂度是O(N*logN).

bubuko.com,布布扣

 

Solution #2:

今天我想出了更好的Solution,只需一遍DFS,可以将时间复杂度优化到O(N),但是空间复杂度同样是O(N).

bubuko.com,布布扣
public class CheckTreeBalanced {
    
    HashMap<TreeNode, Integer> heights = new HashMap<TreeNode, Integer>();

    // The idea is to run DFS once
    boolean isBalanced(TreeNode root){
        if(root == null){
            heights.put(null, 0);
            return true;
        }
        
        if( isBalanced(root.left) && isBalanced(root.right) ){
            if(Math.abs(heights.get(root.left) - heights.get(root.right)) > 1){
                return false;
                
            }else{
                int currentHeight = Math.max(heights.get(root.left),
                        heights.get(root.right)) + 1;
                heights.put(root, currentHeight);
                return true;
            }
            
        }else{
            return false;
        }
    }
}
bubuko.com,布布扣

 

Solution #3:

Cracking the coding interview上看到另一种解法,time complexity O(N), space complexity O(logN). 之所以占用logN的空间是因为这是DFS的特点,整棵树的高度H=logN,DFS必然会占用O(H), explicitly or implicitly.

该算法的思路是基于Solution #1的一种改进,把每个节点的height信息和isBalanced信息融合到一起个变量中:

如果该变量>=0,那么该节点是balanced并且该变量就是节点的height;

如果该变量<0,那么该节点是unbalanced,但同时我们失去了它的height信息。

bubuko.com,布布扣
public class CheckTreeBalanced2 {
    
    public int checkHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        
        int leftHeight = checkHeight(root.left);
        if(leftHeight == -1){
            return -1;
        }
        
        int rightHeight = checkHeight(root.right);
        if(rightHeight == -1){
            return -1;
        }
        
        int heightDiff = leftHeight - rightHeight;
        if(Math.abs(heightDiff) > 1){
            return -1;
        }else{
            return Math.max(leftHeight, rightHeight);
        }
    }
    
    public boolean isBalance(TreeNode root){
        if(checkHeight(root) == -1){
            return false;
        }else{
            return true;
        }
    }
}
bubuko.com,布布扣

 

[Leetcode] Balanced Binary Tree,布布扣,bubuko.com

[Leetcode] Balanced Binary Tree

标签:style   class   blog   code   java   color   

原文地址:http://www.cnblogs.com/Antech/p/3705928.html

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