Binary Tree Vertical Order Traversal - Problem

Imagine looking at a binary tree from above and dropping vertical lines through each node position. Your task is to collect all the nodes that fall on the same vertical line, from left to right, and within each vertical line, collect nodes from top to bottom.

Given the root of a binary tree, return the vertical order traversal of its nodes' values. This means grouping nodes by their horizontal distance from the root, where:

  • Root is at position 0
  • Left children are at position parent - 1
  • Right children are at position parent + 1
  • Nodes in the same column should be ordered top to bottom
  • If two nodes are at the same position (row and column), order them left to right

Example: For a tree with root value 3, left child 9, and right child 20 (with 20 having children 15 and 7), the vertical order would be [[9], [3, 15], [20], [7]]

Input & Output

example_1.py โ€” Basic Tree
$ Input: root = [3,9,20,null,null,15,7]
โ€บ Output: [[9],[3,15],[20],[7]]
๐Ÿ’ก Note: Root 3 is at column 0. Left child 9 at column -1. Right child 20 at column 1. Node 15 (left child of 20) at column 0, and node 7 (right child of 20) at column 2.
example_2.py โ€” Single Node
$ Input: root = [1]
โ€บ Output: [[1]]
๐Ÿ’ก Note: Single node tree results in one column containing just the root value.
example_3.py โ€” Same Column Multiple Nodes
$ Input: root = [1,2,3,4,6,5,7]
โ€บ Output: [[4],[2],[1,5,6],[3],[7]]
๐Ÿ’ก Note: Multiple nodes can share the same column. They are ordered top to bottom, and left to right if at the same level.

Visualization

Tap to expand
๐Ÿ™๏ธ City Planning - Vertical Order TraversalSt. -1St. 0St. 1St. 23Level 1920Level 2157Level 3BFS Queue Process1. Start: Queue[(3, col:0)]2. Process 3 โ†’ Add to St.03. Queue[(9,col:-1),(20,col:1)]4. Process 9 โ†’ Add to St.-15. Process 20 โ†’ Add to St.16. Queue[(15,col:0),(7,col:2)]7. Process 15 โ†’ Add to St.08. Process 7 โ†’ Add to St.2Final Street Map:St.-1: [9]St.0: [3, 15]St.1: [20]St.2: [7]Result: [[9],[3,15],[20],[7]]
Understanding the Visualization
1
Draw Vertical Lines
Imagine vertical grid lines over the tree. Root is at column 0, left children subtract 1, right children add 1
2
BFS Level Traversal
Use BFS to visit buildings level by level (north to south), naturally maintaining proper ordering
3
Group by Streets
As we visit each building, add it to the appropriate street column in our HashMap
4
Collect Results
Traverse from westernmost to easternmost street, collecting all buildings in each column
Key Takeaway
๐ŸŽฏ Key Insight: BFS traversal naturally maintains the required ordering (top-to-bottom, left-to-right) while HashMap efficiently groups nodes by their vertical positions, eliminating the need for sorting.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single BFS traversal visits each node exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

O(n) for HashMap storage + O(w) for BFS queue where w is maximum width

n
2n
โšก Linearithmic Space

Constraints

  • The number of nodes in the tree is in the range [0, 100]
  • Node values are in the range [-100, 100]
  • Follow-up: What if nodes at the same position need different ordering rules?
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28
42.3K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen