题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
解题思路:
计算二叉树高度,在递归过程,判断左右高度差值是否超过1.
public class Solution {
boolean balanced = true;
public boolean IsBalanced_Solution(TreeNode root) {
getHeight(root);
return balanced;
}
public int getHeight(TreeNode node){
if(node==null){
return 0;
}
int left = getHeight(node.left);
int right = getHeight(node.right);
if(Math.abs(left-right)>1){
balanced=false;
}
return Math.max(left,right)+1;
}
}