Pattern Recognition Drill
Medium Trees
The Problem
Return the level-order traversal of a binary tree as list of lists.
What approach would you use?
Think about it before scrolling down.
Use a queue. Process one full level at a time using len(queue) at level start. O(n).
DFS passing depth parameter, append to result[depth]. Works but less intuitive for 'level-order'.
Common Trap
Standard BFS doesn't group by level. The key trick: for each level, process exactly len(queue) nodes.