解法
平衡二叉树的概念 是 左子树和右子树的深度的差的绝对值 等于 1.如果不符合这个条件就是不是平衡二叉树
我们只需要判断左子树和右子树是否符合条件 就OK了。
代码
public class Solution {
public int depth(TreeNode root){
if(root == null) {
return 0;
}
int left = depth(root.left);
if(left == -1){
return -1;
}
int right = depth(root.right);
if( right == -1){
return -1;
}
if( left - right < -1 || left - right > 1 ){
return -1;
}else{
return left>right?left+1:right+1;
}
}
public boolean IsBalanced_Solution(TreeNode root) {
return depth(root) != -1;
}
} 
京公网安备 11010502036488号