35 / 75
Check if tree is height-balanced: at every node, left and right heights differ by at most 1.
Example:
Input:
root=[3,9,20,null,null,15,7]Output:
trueCommon Mistakes:
- Only checking balance at root (must check at every node)
- Recalculating heights multiple times (inefficient O(n²))
- Not propagating unbalanced state up the recursion
- Confusing with complete/perfect binary tree definitions
Notes:
Use -1 as sentinel for unbalanced state. Time O(n), Space O(h). Efficient single-pass solution.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Check if tree is height-balanced: at every node, left and right heights differ by at most 1.
Example:
Input:
root=[3,9,20,null,null,15,7]Output:
trueCommon Mistakes:
- Only checking balance at root (must check at every node)
- Recalculating heights multiple times (inefficient O(n²))
- Not propagating unbalanced state up the recursion
- Confusing with complete/perfect binary tree definitions
Notes:
Use -1 as sentinel for unbalanced state. Time O(n), Space O(h). Efficient single-pass solution.
35/75
Balanced Binary Tree