Skip to content
CodeShare
All problems

Level Order Traversal

Read a binary tree level by level, left to right.

MediumTreesQueue

Given the root of a binary tree, return its values level by level: an array of levels, each listing that level's values from left to right.

Trees are written level by level, with null for a missing child.

Examples

Example 1
Input
root = [3, 9, 20, null, null, 15, 7]
Output
[[3], [9, 20], [15, 7]]
Example 2
Input
root = [1]
Output
[[1]]
Example 3
Input
root = []
Output
[]

Constraints

  • The tree has between 0 and 2000 nodes.
  • -1000 <= Node.val <= 1000