Balanced Binary Tree

超简洁的代码

本来考虑是不是真的要每个点求一个maxDepth,看来是的哟

public class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root==null) return true;
        if(Math.abs(maxDepth(root.right)-maxDepth(root.left))>1) return false;
        return isBalanced(root.left)&& isBalanced(root.right);
    }
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        return(1+ Math.max(maxDepth(root.left),maxDepth(root.right)));
    }
}

 

posted @ 2015-06-10 05:38  世界到处都是小星星  阅读(120)  评论(0编辑  收藏  举报