Skip to content
Onsite

Binary Tree Level Order Traversal

MediumP1Coding

Given the root of a binary tree, return its level-order traversal: the node values level by level, from left to right, as a list of lists. An empty tree gives an empty list.

Tree nodes have val, left and right. The class TreeNode is already defined for you. The tree arrives in level-order form (as in the examples) and you receive its root, which is null for an empty tree.

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 number of nodes is in the range [0, 2000].
  • -1000 <= Node.val <= 1000

Hints

Run checks the 3 visible tests, Submit all 9.