/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
boolean m,n;
int x,y;
if(root==null) return true;
x=f(root.left);
y=f(root.right);
if(-1<=x-y&&x-y<=1){
m=isBalanced(root.left);
n=isBalanced(root.right);
return m&&n;
}else return false;
}
public int f(TreeNode root){
int m,n;
if(root==null) return 0;
m=f(root.left);
n=f(root.right);
return Math.max(m,n)+1;
}
}