#C13878. Binary Tree Level Order Traversal
Binary Tree Level Order Traversal
Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its node values. In level order traversal, nodes are visited level-by-level from left to right.
The tree is provided as a string representation in level order with missing nodes denoted by null
. For example, the tree
3 / \ 9 20 / \ 15 7
is represented as [3,9,20,null,null,15,7]
.
Your task is to construct the tree from the input, perform a level order traversal, and output the traversal as a nested list. The output for the above example is [[3],[9,20],[15,7]]
.
inputFormat
Input is given in a single line through standard input. The line contains a string representation of a binary tree in level order enclosed in square brackets. Nodes are separated by commas, and a missing node is represented by the string null
. For example: [3,9,20,null,null,15,7]
.
outputFormat
Output the level order traversal of the tree as a nested list printed to standard output. For example, for the input [3,9,20,null,null,15,7]
, the output should be [[3],[9,20],[15,7]]
.
[]
[]