class Solution {
public boolean isBalanced(TreeNode root) {
return depth(root) != -1;
}
public int depth(TreeNode node){
if(node==null){
return 0;
}
int l_depth = depth(node.left);
if(l_depth==-1){
return -1;
}
int r_depth = depth(node.right);
if(r_depth==-1){
return -1;
}
if(Math.abs(l_depth-r_depth)>1){
return -1;
}else{ //满足平衡二叉树的条件
return Math.max(l_depth,r_depth)+1;
}
}
}