Given the root of a binary tree, calculate its vertical order traversal. The root is at (row 0, col 0); a node at (row, col) has its left child at (row + 1, col - 1) and its right child at (row + 1, col + 1). Return a list with one entry per column from the leftmost to the rightmost, each listing that column's values from top to bottom, with nodes that share the same row and column ordered by value.
Given the root of a binary tree, calculate its vertical order traversal. The root is at (row 0, col 0); a node at (row, col) has its left child at (row + 1, col - 1) and its right child at (row + 1, col + 1). Return a list with one entry per column from the leftmost to the rightmost, each listing that column's values from top to bottom, with nodes that share the same row and column ordered by value.
Example 1:
Input: {"root":[1,2,3,4,6,5,7]}
Output: [[4],[2],[1,5,6],[3],[7]]
Constraints:
The number of nodes in the tree is in the range [1,1000].
0≤ Node.val ≤1000
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"root":[1,2,3,4,6,5,7]}
Start a BFS at the root 1, which sits at row 0, column 0. Going left subtracts 1 from the column, going right adds 1.
-2-10121234657
triples (col, row, val) in BFS order
none yet
queue
1@0
current
in queue
recorded
placed in its column
Shaded stripes are columns; numbers on top are column indexes.