Approach hintTry it first, then revealHide
Compute heights bottom-up and return a sentinel such as -1 as soon as any subtree is unbalanced, so each node is visited once instead of recomputing heights from every ancestor. O(n).
Compute heights bottom-up and return a sentinel such as -1 as soon as any subtree is unbalanced, so each node is visited once instead of recomputing heights from every ancestor. O(n).