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.

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?

Visualization

Tap to expand
Binary Tree Vertical Order Traversal INPUT Binary Tree Structure 3 col=0 9 col=-1 20 col=1 15 col=0 7 col=2 [3,9,20,null,null,15,7] ALGORITHM (BFS) 1 Initialize BFS Queue Start with (root, col=0) 2 Process Level by Level Track column for each node 3 Update Columns Left: col-1, Right: col+1 4 Group by Column Sort columns, output groups Column Mapping: -1:[9] 0:[3,15] 1:[20] 2:[7] Vertical lines: -1, 0, 1, 2 -1 0 1 2 FINAL RESULT Grouped by Vertical Column Col -1 9 Col 0 3 15 Col 1 20 Col 2 7 Left to Right order Output: [[9],[3,15],[20],[7]] OK - Done! 4 vertical columns found Top-to-bottom order preserved Key Insight: BFS naturally processes nodes level by level (top-to-bottom), which is exactly what we need for vertical order traversal. By tracking each node's column index (root=0, left=col-1, right=col+1), we can group nodes by their vertical position and maintain the correct top-to-bottom order within each group. TutorialsPoint - Binary Tree Vertical Order Traversal | BFS Approach
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