首页 > 其他 > 详细

Balanced Binary Tree

时间:2015-02-26 09:44:42      阅读:228      评论:0      收藏:0      [点我收藏+]

 

 

 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 }

 

Balanced Binary Tree

原文:http://www.cnblogs.com/diyishao/p/4300591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!