输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null)return true;
int leftD = TreeDepth(root.left);
int rightD = TreeDepth(root.right);
int diff = leftD - rightD;
if(diff > 1 || diff < -1)
return false;
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
private int TreeDepth(TreeNode root) {
if(root == null)return 0;
int leftD = TreeDepth(root.left);
int rightD = TreeDepth(root.right);
return Math.max(leftD+1,rightD+1);
}
}