Practice/Meta/Leetcode 314. Binary Tree Vertical Order Traversal
CodingMust
You are given the root of a binary tree. Your task is to perform a column-by-column traversal and return the values of all nodes grouped by their vertical position.
Imagine drawing a vertical line through the root node (column 0). Moving to a left child decreases the column index by 1, while moving to a right child increases it by 1. Return a list where each element contains all node values in that column, ordered from the leftmost column to the rightmost column.
Within each column, nodes should appear in the order they would be visited during a level-order (top-to-bottom) traversal. If multiple nodes exist at the same position (same column and level), they should appear in left-to-right order as encountered during the traversal.
Example 1:
`
Input: root = [3,9,20,null,null,15,7]
3
/
9 20
/
15 7
Output: [[9],[3,15],[20],[7]] Explanation:
Example 2:
`
Input: root = [1,2,3,4,5,6,7]
1
/
2 3
/ \ /
4 5 6 7
Output: [[4],[2],[1,5,6],[3],[7]] Explanation:
Example 3:
Input: root = [] Output: [] Explanation: Empty tree has no columns