PPractick

DSA interview question

Binary Tree Level Order Traversal

Easy · Data structures · Topics: tree, bfs, queue

The problem

An org-chart viewer displays a company tree level by level: the CEO, then all direct reports, then their reports, and so on, each level as its own row. For a binary tree, that's a breadth-first search with a queue, where each iteration of the outer loop processes exactly the nodes currently in the queue, which are exactly one level.

Example: [3, 9, 20, null, null, 15, 7] → [[3], [9, 20], [15, 7]].

Function signature

vector<vector<int>> levelOrder(TreeNode* root);

Test cases

Input (level order)OutputExplanation
[3, 9, 20, null, null, 15, 7][[3], [9, 20], [15, 7]]
[1][[1]]
[][]
[1, 2, 3, 4, null, null, 5][[1], [2, 3], [4, 5]]Gaps don't matter
[1, 2, null, 3][[1], [2], [3]]A left chain

(Confirm: each inner list lists one level from left to right.)

How interviewers phrase it

Clarifying questions to ask

  1. One list per level, or a flat list? (One per level.)
  2. Left to right within a level? (Yes; zigzag is a separate problem.)
  3. Can the tree be empty? (Yes, so return an empty list.)

Follow-ups to expect

  1. "How do you separate levels?"
  2. "DFS alternative?"
  3. "Related variations?"
  4. "Space?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in