递归方法
public class Solution {
private boolean ans=true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return ans;
}
public int maxDepth(TreeNode root){
if(root==null)return 0;
int l=maxDepth(root.left);
int r=maxDepth(root.right);
if(Math.abs(l-r)>1)ans= false;
return Math.max(l,r)+1;
}
}