题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null)
return true;
if(!IsBalanced_Solution(root.left))
return false;
if(!IsBalanced_Solution(root.right))
return false;
int left = depth(root.left);
int right = depth(root.right);
if(Math.abs(left - right) <= 1)
return true;
else
return false;
}
private int depth(TreeNode root) {
if(root == null)
return 0;
return Math.max(depth(root.left), depth(root.right)) + 1;
}
}