Back to Practice
#0057

Binary Tree Level Order Traversal

MediumDSA25 min20 XP

Problem

Given the root of a binary tree, return a list of levels, where each level is a list of node values from left to right.

Why This Matters

Level-order traversal is the standard tree BFS pattern. It teaches queue processing and how to separate one layer from the next.

Function Signature

def level_order(root):

Examples

Example 1
Inputroot = 3 with children 9 and 20; 20 has children 15 and 7
Output[[3], [9, 20], [15, 7]]

Each inner list contains one horizontal level of the tree.

Constraints

  • Return [] for an empty tree.
  • Preserve left-to-right order inside each level.
  • Use BFS or an equivalent level-aware traversal.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Three-level tree
Input3 / 9,20 / 15,7
Expected[[3], [9, 20], [15, 7]]

The queue processes one level at a time.

Hidden Test Categories
Empty treeOnly left childrenOnly right children