Binary Tree Vertical Order Traversal

Given the root of a binary tree, return its vertical order traversal from left column to right column. Pattern focus: BFS Level Order with Queue. Track each node's column while traversing level by level.

Input Format

root = binary tree root

Output Format

vertical order traversal as list of columns

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • root must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [3,9,20,null,null,15,7]

Output:

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

Explanation:

Nodes are grouped by vertical column from left to right.

Example 2:

Input:

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

Output:

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

Explanation:

Columns are ordered from the leftmost to the rightmost.

Loading...
Binary Tree Vertical Order Traversal