AlgoFrames

Level order

Standard · Write code · about 10 min

Given the root of a binary tree, return its values level by level. Each level is a list of values from left to right, and the levels go from the root down.

Example 1

Input:

[[3,9,20,null,null,15,7]]

Output:

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

Level 0 is [3], level 1 is [9, 20], level 2 is [15, 7].

Example 2

Input:

[[1,2,3,4,5,null,6]]

Output:

[[1],[2,3],[4,5,6]]

Constraints

  • 0 ≤ number of nodes ≤ 1,000
  • -1,000 ≤ Node.val ≤ 1,000
  • The tree is given in level order; null marks a missing child

Learn the idea first: Level-order traversal, a visual lesson.

More Level-order traversal problems