#102

Binary Tree Level Order Traversal

Medium BFS by levels

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

?The problem

Return the node values level by level, from left to right.

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

◎Understanding

Really asking: values row by row.

Why this works: BFS with a queue; the queue size at the start of a round is exactly one level.

Example: 3 (9, 20 (15, 7))

[3], [9, 20], [15, 7]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using queue.size() in the for-loop condition — it changes as you add children.

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