输入一棵二叉树,判断该二叉树是否是平衡二叉树。
递归
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null)
return true;
int left = depth(root.left);
int right = depth(root.right);
if(Math.abs(left-right) > 1)
return false;
return true;
}
private int depth(TreeNode root)
{
if(root == null)
return 0;
int left = depth(root.left);
int right = depth(root.right);
return (left > right) ? (left+1) : (right+1);
}