← DiffPush

Vertical Order Traversal of Binary Tree

DiffPush Tier Binary Trees · Medium Problems O(n log n) · O(n)

The Skyscraper Column Census

A skyline model arranges buildings as a binary tree: each building sits one floor below its parent, with children slotted one column left and one column right. The city surveyor photographs the model from directly above and reads each column top to bottom. When two buildings share a column and floor, the readout lists them in ascending identifier order — no exceptions, even if construction order says otherwise.

Input: The root of a binary tree given as a level-order array where null marks a missing child. The root is at column 0 and row 0.

Output: Return a list of lists: columns ordered left to right, each column's values sorted by row, then by value for ties.

Constraints

Examples

Example 1

Input: {"tree":[3,9,20,null,null,15,7]}
Output: [[9],[3,15],[20],[7]]
Column -1 holds 9; column 0 reads 3 above 15; columns 1 and 2 hold 20 and 7.

Example 2

Input: {"tree":[1,2,3,4,5,6,7]}
Output: [[4],[2],[1,5,6],[3],[7]]
Column 0 lists 1 (row 0) before 5 and 6 (row 2), which tie by row.

Solve this in your browser →

Also on LeetCode ↗