给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
答案:
public class Solution {
private boolean flag;
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) {
return true;
}
flag = true;
getdepth(root);
return flag;
}
public int getdepth(TreeNode root) {
if(root == null) {
return 0;
}
int l = getdepth(root.left);
int r = getdepth(root.right);
if(Math.abs(l - r) > 1) {
flag = false;
return -1;
}
return Math.max(l, r) + 1;
}
}



