PIXELBANKv8.2.1
Menu

Binary Tree Level Order Traversal

MediumTrees

Given a binary tree (level-order array), return the level order traversal as each level on a separate line.

Output each level space-separated.

Example:

Input:
3,9,20,null,null,15,7
Output:
3
9 20
15 7
Reasoning:
  • The binary tree is constructed from the level-order array: the first element 3 is the root, the next two elements 9 and 20 are its children, and the last two elements 15 and 7 are the children of 20.
  • We start the level order traversal from the root 3, which is the first level and only contains the value 3.
  • The next level consists of the root's children, 9 and 20, which are output as 9 20.
  • The final level consists of the children of 20, which are 15 and 7, output as 15 7.

Constraints:

  • 0 <= number of nodes <= 2000
  • -1000 <= Node.val <= 1000
Editor

Test Results

0/0
Run code to see test results.