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