PIXELBANKv9.1.0
Menu

Binary Tree Level Order Traversal

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
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.