题解 | #平衡二叉树#
平衡二叉树
http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
public class Solution {
public static boolean flag = true;
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null){
flag = true;
}
isBalanced(root);
return flag;
}
public int isBalanced(TreeNode root){
if(root == null){
return 0;
}
int left = isBalanced(root.left);
int right = isBalanced(root.right);
if(left == -1 || right == -1 || Math.abs(left - right) > 1){
flag = false;
return -1;
}
return Math.max(left, right) + 1;
}
} 