Vertical Order Traversal
Given the root of a binary tree, return its vertical order traversal. Assign the root coordinate (row=0, col=0); a left child is (row+1, col-1) and a right child is (row+1, col+1). Group nodes by column from leftmost to rightmost. Within a column, order nodes from top row to bottom row; when two nodes share the same row and column, order them by ascending value. Return one list of values per column.
Open official problem prompt ↗Produce, for each vertical column of the tree, the list of node values read top-to-bottom, breaking ties within a cell by value.
Imagine dropping every node straight down onto a number line marked by column index. Nodes landing in the same column form a stack; you read each stack from the highest node down, and if two land at the exact same spot you place the smaller number first.
- Input
- root = [3,9,20,null,null,15,7]
- Output
- [[9],[3,15],[20],[7]]
- Why
- 9 sits at col -1; 3 (row 0) and 15 (row 2) share col 0 and are ordered top-down; 20 at col 1; 7 at col 2.
The number of nodes is in the range [1, 1000]0 <= Node.val <= 1000