题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
思路:
平衡二叉树:要么为一颗空树,要么左子树、右子树都是平衡二叉树,且左子树和右子树高度只差绝对值不能超过1。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root==null)
return true;
if(Math.abs(height(root.left)-height(root.right))>1)
return false;
return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
}
private int height(TreeNode node){
if(node==null)
return 0;
return Math.max(height(node.left),height(node.right))+1;
}
}