Balanced Binary Tree

 

 

 1 public class Solution {
 2     public boolean isBalanced(TreeNode root) {
 3         int result = checkHeight(root);
 4         return result != -1;
 5     }
 6     
 7     private int checkHeight(TreeNode root) {
 8         if(root == null) return 0;
 9         int left = checkHeight(root.left);
10         if(left == -1) return -1;
11         int right = checkHeight(root.right);
12         if(right == -1) return -1;
13         if(Math.abs(left - right) > 1) {
14             return -1;
15         } else {
16             return Math.max(left, right) + 1;
17         }
18     }  
19 }

 

posted @ 2015-02-26 09:35  江南第一少  阅读(110)  评论(0)    收藏  举报