```
public class Solution {
public boolean isBalanced(TreeNode root){
int hi = height(root);
boolean res = hi!=-1;
return res;
}
private int height(TreeNode subtree) {
//if this subtree is balanced return its height, otherwise return -1;
if (subtree == null) return 0;
if (subtree.left==null && subtree.right==null) {
return 1;
}
int a,b;
if (subtree.left!=null) {
a = height(subtree.left);
} else {
a = 0;
}
if (subtree.right!=null) {
b = height(subtree.right);
} else {
b = 0;
}
if (a==-1 || b == -1) return -1;
else {
if (Math.abs(a-b)<=1) {
return Math.max(a, b) + 1;
} else {
return -1;
}
}
}
```

}

This is my code