#110

Balanced Binary Tree

Easy Height with early exit

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether every node's left and right subtree heights differ by at most 1.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: do the two subtree heights differ by at most 1 at every node?

Why this works: return the height, or -1 as soon as any subtree is unbalanced, so you never recompute.

Example: 1 (2 (3 (4)), -)

node 2: heights 2 vs 0 → -1 → whole tree unbalanced

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Checking only the root's two subtrees — every node must be balanced.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(h).
Topic: Binary Trees
🎬 Episodes 53–60: video coming soon