/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null)
return true;
if (!isBalanced(root.left) || !isBalanced(root.right))//递归判断左右子树是否是平衡二叉树
return false;
if (Math.abs(maxDepth(root.left) - maxDepth(root.right)) > 1 )//判断左子树高度与右子树高度差是否大于1
return false;
return true;
}
//计算树的深度的函数
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
int ld = maxDepth(root.left);
int rd = maxDepth(root.right);
if(ld > rd)
return ld+1;
else
return rd+1;
}
}