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

Balanced Binary Tree

时间:2015-02-26 09:47:51      阅读:103      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!