Back to Practice
#0238

Binary Tree Level Order Traversal

MediumDSA28 min20 XP

Problem

A tree node is [value, left, right]. Return a list of levels, where each level is a list of values from left to right.

Why This Matters

Level-order traversal is the tree version of BFS and is the gateway to shortest-path and layer-by-layer reasoning.

Function Signature

def level_order(root):

Examples

Example 1
Inputroot = [3, [9, None, None], [20, [15, None, None], [7, None, None]]]
Output[[3], [9, 20], [15, 7]]

Each BFS layer becomes one list in the answer.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Three levels
Input[3, [9, None, None], [20, [15, None, None], [7, None, None]]]
Expected[[3], [9, 20], [15, 7]]

The queue processes nodes by level.

Empty tree
InputNone
Expected[]

No nodes means no levels.

Hidden Test Categories
single nodeleft-heavy treeright-heavy tree