首页 > 其他 > 详细

110. Balanced Binary Tree

时间:2016-06-10 14:51:19      阅读:175      评论:0      收藏:0      [点我收藏+]

helper函数,如果不满足条件就返回-1,如果任一子树不满足,即始终返回-1

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

 

110. Balanced Binary Tree

原文:http://www.cnblogs.com/warmland/p/5573599.html

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