题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
import java.lang.Math;
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(null == root){
return true;
}
if(Math.abs(treeDepth(root.left) - treeDepth(root.right)) > 1){
return false;
}
return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
}
public int treeDepth(TreeNode root) {
if(null == root){
return 0;
}
int left = treeDepth(root.left);
int right = treeDepth(root.right);
return (left > right ? left : right) + 1;
}
}