输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root==null) return true;
int dis = Math.abs(getlen(root.left)-getlen(root.right));
if(dis>1)
return false;
else
return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
}
public int getlen(TreeNode root)
{
if(root ==null)
return 0;
int l = getlen(root.left);
int r = getlen(root.right);
return Math.max(l,r)+1;
}
}