Hard · Trees

Vertical Order Traversal of a Binary Tree

Given the root of a binary tree, return its vertical order traversal. The root is at row 0, column 0; a left child is at (row + 1, column − 1) and a right child at (row + 1, column + 1). List the columns from leftmost to rightmost. Within a column, order the values by row from top to bottom, and put the smaller value first when two nodes share the same row and column.

Examples

Example 1

[3,9,20,·,·,15,7]

Output: [9] [3,15] [20] [7]

Example 2

[1,2,3,4,6,5,7]

Output: [4] [2] [1,5,6] [3] [7]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems