858. Vertical Order Traversal

EasyTreesBinary TreeBFSHash

Given the root of a binary tree, return its vertical order traversal — nodes grouped by their horizontal distance (column), columns ordered left to right, and within each column ordered top to bottom by level-order arrival. Return the result as a nested array. The root is column 0; left child is column minus one, right child plus one.

Input: A level-order array of the tree.

Output: Nested array — values grouped by column.

Examples

Example 1
Input: [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]
Explanation: Columns from left to right.
Example 2
Input: [1]
Output: [[1]]
Explanation: Single column.
Example 3
Input: []
Output: []
Explanation: Empty tree.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →