public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root==null)
return true;
int l=TreeDepth(root.left);
int r=TreeDepth(root.right);
int temp=l-r;
if(temp>1||temp<-1)//根据平衡二叉树的性质,左右两个子树的高度差的绝对值不超过1
return false;
return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
}
//求树的深度
public int TreeDepth(TreeNode root){
if(root==null)
return 0;
int left=1;
int right=1;
left=left+TreeDepth(root.left);
right=right+TreeDepth(root.right);
return left>right?left:right;
}
}