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