Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.levelOrder(root). Return one list of values for each depth from left to right. Return an empty list for an empty tree.

Starter code

class Solution:
    def levelOrder(self, root):
        pass
Test cases

three-levels

{
  "args": [
    {
      "$type": "binary-tree",
      "values": [
        3,
        9,
        20,
        null,
        null,
        15,
        7
      ]
    }
  ]
}

Expected: [[3],[9,20],[15,7]]

Wizard outline
  1. Step 1: Initialize Solution.levelOrder

    Replace the empty starter with the first real state owned by Solution.levelOrder. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Pass the Empty case

    Complete the readable core algorithm for one representative Interview case. Use the queue size at the start of each round to define one tree level.

  3. Step 3: Harden the Three Levels boundary

    Repair the reviewed boundary and pass the complete submission contract. The queue initially contains exactly the current level; consuming that fixed count records every node at that depth while enqueuing precisely the next level.

Footguns and prerequisites
  • Do not let children appended during a round extend that same level.
  • trees and graphs
Reviewed references
Practice prerequisites
  • Expand One Tree Level(opens in a new tab)

    Expand One Tree Level isolates at the start of each outer iteration, the queue contains exactly the current level in left-to-right order. That focused state discipline is required when implementing binary tree level order as a complete Interview Problem.

Recommended approach and implementation

Run breadth-first search with a deque and consume exactly the queue length captured at the start of each level.

Why it works: The queue initially contains exactly the current level; consuming that fixed count records every node at that depth while enqueuing precisely the next level.

from collections import deque

class Solution:
    def levelOrder(self, root):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        if root is None:
            return []
        result = []
        queue = deque([root])
        while queue:
            level = []
            for _ in range(len(queue)):
                node = queue.popleft()
                level.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            result.append(level)
        return result