2026-11-07 22:51 UTC 1082 bytes
raw + new
# BFS Level Order (Practice 4875): Pattern

Interviewers love bfs level order because it checks whether you can state the invariant before coding. Here is a compact rehearsal note.

## Approach

Queue nodes; process level by level collecting values.

```python
from collections import deque
def level_order(root):
    if not root: return []
    q, out = deque([root]), []
    while q:
        level = []
        for _ in range(len(q)):
            n = q.popleft(); level.append(n.val)
            if n.left: q.append(n.left)
            if n.right: q.append(n.right)
        out.append(level)
    return out
```

## Complexity

Linear in the dominant input size for the scan; heap/tree variants add a log factor.

## Interview pitfalls

State the invariant out loud, name the complexity, then code. Skipping the invariant is the usual fail mode.

## Practicing out loud

Narrate the approach, complexity and one edge case in under a minute. Disclosure: I work on Linkjob AI, a real-time interview assistant. If you want live practice help, see [Linkjob AI](https://www.linkjob.ai/).
link copied