#C7366. Vertical Order Traversal of a Binary Tree
Vertical Order Traversal of a Binary Tree
Vertical Order Traversal of a Binary Tree
You are given a binary tree in level order format. Your task is to perform a vertical order traversal of its nodes and output the values in each vertical column from leftmost column to rightmost column.
The vertical order traversal is defined as follows:
Let the column of the root be \(0\). For any node at column \(c\), its left child is at column \(c-1\) and its right child is at column \(c+1\). Collect the nodes in the same column together in the order as encountered in a level order (BFS) traversal.
For example, consider the tree:
3 / \ 9 20 / \ 15 7
The vertical order traversal would be: [[9], [3, 15], [20], [7]].
inputFormat
The input is provided as a single line from standard input. It contains the values of the nodes in level order, separated by spaces. If a node is missing, the token null
is used to indicate the absence of a child.
For example: 3 9 20 null null 15 7
outputFormat
The output is the vertical order traversal represented as a list of lists printed in one line. Each sub-list corresponds to a vertical column starting from the leftmost column to the rightmost column.
For example: [[9], [3, 15], [20], [7]]
1
[[1]]
</p>