题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean isBalanced = true;
public boolean IsBalanced_Solution(TreeNode root) {
getDepth(root);
return isBalanced;
}
public int getDepth(TreeNode root){
if(root==null){
return 0;
}
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
if((leftDepth-rightDepth>1)||(leftDepth-rightDepth<-1)){
isBalanced = false;
}
return leftDepth>rightDepth?leftDepth+1:rightDepth+1;
}
}