```
public boolean isBalanced(TreeNode root) {
return -1 != check(root);
}
private int check(TreeNode root){
if (root == null) return 0;
int l = check(root.left);
int r = check(root.right);
return l == -1 || r == -1 || Math.abs(l-r) > 1 ? -1 : 1 + Math.max(l,r);
}
```