Binary Tree Level Order Traversal

Medium Trees AI round

root is the root of a binary tree with at least one node.

The tree's levels group its nodes by their depth: the root alone is the first level, its children make the second, their children the third, and so on down to the deepest nodes.

Return the values of the nodes level by level, from the root's level down, each level as a list of its values read from left to right.

Example 1

Input: root = [4,9,1,null,7,3,8,2]
Output: [[4],[9,1],[7,3,8],[2]]
4917382

4, the root, is the first level alone. Its children 9 and 1 make the second. Below them, 7, 3 and 8 make the third, read from left to right: 7 hangs to the right of 9, and 3 and 8 under 1. 2, under 7, is the last level.

Example 2

Input: root = [0,-6,6,-9,null,null,9]
Output: [[0],[-6,6],[-9,9]]
0-66-99

Example 3

Input: root = [5,null,-3,6]
Output: [[5],[-3],[6]]
5-36

Constraints

  • 1 ≤ number of nodes ≤ 10⁵
  • -10⁴ ≤ node.val ≤ 10⁴

Watch the explanation

More Trees

All Trees problems