Binary Tree Level Order Traversal
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google reported this one in September 2026, and the detail that matters is the output shape: a list of lists, one inner list per depth, left to right. If your OA invite is sitting in your inbox, this is a classic tree question wearing a simple costume. It's breadth-first search on a binary tree, and the empty-tree case returns an empty list, not a list holding an empty list. Up to 100000 nodes means the skewed tree in Example 2 is a real test case. If your mind goes blank mid-assessment, StealthCoder runs invisibly on your screen as a safety net and hands you the solution in real time.
The problem
Given the root of a binary tree, return its node values grouped by depth from top to bottom. Values within each level must appear from left to right. Return an empty list for an empty tree. Function levelOrder(root: TreeNode) → List<List<Integer>> Examples Example 1 root = [3,9,20,null,null,15,7] return = [[3],[9,20],[15,7]] The root forms level zero, its two children form level one, and nodes 15 and 7 form level two. Example 2 root = [1,null,2,3] return = [[1],[2],[3]] Each node occupies a separate depth even though the tree is skewed. Example 3 root = [] return = [] An empty tree has no levels. Constraints The tree contains at most 100000 nodes. Each node value is between -10^9 and 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is processing the queue one level at a time. Push the root, then loop while the queue isn't empty. At the top of each iteration, record the queue's current size. That number is exactly how many nodes belong to this level. Pop that many, collect their values, and enqueue their left then right children. Append the collected list to the result. Pitfalls: forgetting the size snapshot and mixing levels, returning [[]] for an empty tree, and using recursion on a skewed tree with 100000 nodes, which can blow the stack. A DFS with a depth parameter works too, but it carries that same recursion risk. Use a deque, not a list with pop(0), so each pop is O(1). Total time is O(n), space is O(n). If you freeze on the size snapshot detail during the live OA, StealthCoder is the hedge that gets you unstuck.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Binary Tree Level Order Traversal cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as binary tree level order traversal. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Binary Tree Level Order Traversal FAQ
How hard is Binary Tree Level Order Traversal really?+
It's a standard medium-level tree problem, and most people find it easy once they know BFS. The whole solution is about fifteen lines. The only real risk is fumbling the per-level grouping or the empty-tree return under time pressure.
What's the trick to grouping nodes by level?+
Snapshot the queue length at the start of each loop iteration. That count is the number of nodes in the current level. Pop exactly that many, collect their values, and push their children. The next iteration then starts cleanly on the next level.
Should I use BFS or DFS for this?+
BFS with a queue is the natural fit and the safest. DFS works if you pass a depth and append into result[depth], creating new lists as needed. With up to 100000 nodes, a skewed tree could overflow the recursion stack, so BFS is safer.
What edge cases show up in the tests?+
The empty tree must return an empty list. A skewed tree like [1,null,2,3] must give one value per level. Also expect negative values and large magnitudes up to 10^9, which don't affect the logic but confirm you aren't assuming positives.
How do I prepare for this in 48 hours?+
Write the BFS version from memory twice, once in your OA language. Then try the DFS depth variant and a zigzag twist. Know the complexity, O(n) time and O(n) space, so you can explain it. Don't cram new topics. Get this pattern automatic.