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