Question bankPricingSign in

Binary Tree Level Order Traversal

TreesMedium1:30

Given a binary tree, explain how you would return its level order traversal: the values of nodes at each level grouped together, from left to right.

For example:

      3
     / \
    9  20
      /  \
     15   7
Output: [[3], [9, 20], [15, 7]]

How do you keep track of which nodes belong to which level? What data structure is most suitable here?

Explain your approach and analyze the time and space complexity.

How to approach it

  • Hint 1

    BFS (breadth-first search) naturally processes nodes level by level.

  • Hint 2

    Use a queue. At each level, note how many nodes are currently in the queue - that is the number of nodes at this level.

  • Hint 3

    Process exactly that many nodes, adding their children to the queue for the next level. Collect values into a list for the current level.

Ready to answer it out loud?

Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.

Practising needs an invite code. Join the waitlist and we’ll send you one.