We determine recursively the height of the root node but when the recursion is coming upwards we return UNBALANCED instead of the actual height if we know that the tree is already known to be unbalanced.

We visit each node just once thus it has linear time complexity.

```
private static final int UNBALANCED = -99;
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
return getHeight(root) != UNBALANCED;
}
private int getHeight(TreeNode root) {
if (root == null) {
return -1;
}
int l = getHeight(root.left);
int r = getHeight(root.right);
if (l == UNBALANCED || r == UNBALANCED || Math.abs(l-r) > 1) {
return UNBALANCED;
}
return 1 + Math.max(l,r);
}
```