#C2667. Binary Tree Level Order Traversal
Binary Tree Level Order Traversal
Binary Tree Level Order Traversal
Given a binary tree, your task is to return its level order traversal. In a level order traversal, the nodes are visited level by level from left to right.
The binary tree is provided as a space‐separated list in its level order representation. Each token in the input is either an integer or the word null
(which indicates that there is no node at that position in the tree). For example, the input 3 9 20 null null 15 7
represents the binary tree:
\(\begin{array}{c}3\\{9 \quad 20}\\\ \quad 15 \quad 7\end{array}\)
Your program should output the level order traversal as a list of lists. Each inner list represents one level in the tree.
Note: If the tree is empty, output an empty list []
.
inputFormat
The input consists of a single line containing the space separated tokens representing the level order traversal of the binary tree. Each token is either an integer or the string null
indicating an absent node.
Example:
3 9 20 null null 15 7
outputFormat
Output the level order traversal of the tree as a nested list in the following format:
[[3],[9,20],[15,7]]
The output should be printed to standard output.
## sample1
[[1]]