```
public boolean isBalanced(TreeNode root) {
int res = heightOfTree(root);
return res != -1;
}
// if the tree is balanced , return the height of the tree
// if the tree is unbalanced, return -1 to denote it isn't balanced
public int heightOfTree(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
int left = heightOfTree(root.left);
int right = heightOfTree(root.right);
if (left == -1 || right == -1 || Math.abs(left - right) > 1) {
return -1;
}
return 1 + Math.max(left, right);
}
```