题目
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树
平衡二叉树(Balanced Binary Tree),具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
思想
递归
代码
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
return getDepth(root) != -1;
}
public int getDepth(TreeNode root)
{
if(root == null)
return 0;
int leftDepth = getDepth(root.left);
if(leftDepth == -1)
return -1;
int rightDepth = getDepth(root.right);
if(rightDepth == -1)
return -1;
return Math.abs(leftDepth-rightDepth)>1?-1:1+Math.max(leftDepth,rightDepth);
}
}