//也就是把高度在递归过程中给一并算了。
public class Balance {
public static boolean checkBalance(TreeNode root, int[] dep){//java 里没有传地址
if(null == root){
dep[0] = 0;
return true;
}
int[] leftDep = new int[1];
int[] rightDep = new int[1];
boolean leftBalance = checkBalance(root.left, leftDep);
boolean rightBalance = checkBalance(root.right, rightDep);
dep[0] = Math.max(leftDep[0], rightDep[0]) + 1;
return leftBalance && rightBalance && (Math.abs(rightDep[0] - leftDep[0]) <=1);
}
public boolean isBalance(TreeNode root) {
// write code here
int[] dep = new int[1];
return checkBalance(root, dep);
}
}