根据平衡二叉树的定义,一颗平衡二叉树的左右子树都是平衡二叉树且他们的高度差不超过1
注:空树是平衡二叉树
public boolean IsBalanced_Solution(TreeNode root) {
if(root==null) return true;
return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right)&&
(Math.abs(TreeDepth(root.left)-TreeDepth(root.right))<=1);
}
private int TreeDepth(TreeNode root) {
if(root==null)return 0;
return Math.max(TreeDepth(root.left)+1,TreeDepth(root.right)+1);
}
京公网安备 11010502036488号