987. Vertical Order Traversal of a Binary Tree
Asked at Salesforce
Difficulty
Hard
Time
O(n log n)
Space
O(n)
Pattern
Hash Table
Companies
1
Problem
You are given the root of a binary tree. Imagine placing each node on a grid. The root sits at position (row, col) = (0, 0). A left child moves one step down and one step left, so it is at (row + 1, col - 1). A right child moves one step down and one step right, so it is at (row + 1, col + 1).
Now look at the tree column by column. Start with the leftmost column and move right to the rightmost column. Inside one column, list the nodes from top to bottom (smaller row first).
Sometimes two nodes land on the exact same (row, col) cell. When that happens, put the smaller value first. Return the answer as a list of lists: one inner list per column, in left-to-right order.
Columns and rows on a grid
Constraints
- ●
The number of nodes in the tree is in the range [1, 1000]. - ●
0 <= Node.val <= 1000
Examples
root = [3,9,20,null,null,15,7][[9],[3,15],[20],[7]]root = [1,2,3,4,5,6,7][[4],[2],[1,5,6],[3],[7]]root = [1,2,3,4,6,5,7][[4],[2],[1,5,6],[3],[7]]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Salesforce | Hard | View all Salesforce questions → |
How to Think About It
Use DFS or BFS to assign column and row coordinates to every node
Store nodes in a map keyed by column, then by row within each column
For nodes at the same column and row, sort them by value before adding to result
BFS is preferred to naturally process nodes top-to-bottom within each column
Collect all (col, row, value) triples and sort by col, then row, then value
Optimal Approach
Perform a BFS or DFS traversal, tracking the column and row of each node. Store each node as a tuple of (column, row, value) in a list. Sort the list by column first, then by row, then by value. Group consecutive entries with the same column into sub-lists to form the result. BFS is preferred because it naturally processes nodes level by level, giving a more intuitive row ordering. The key insight is that three-dimensional sorting (col, row, value) produces the correct output.
Collect triples, then sort
What Trips People Up in Real Interviews
Clarify the tie-breaking rule: same column and row means sort by value
Ask whether it is vertical traversal (column-first) or top-down (row-first)
Discuss BFS vs DFS: BFS gives natural row ordering, DFS does not
Mention that column can be negative (left subtree), zero (root), positive (right)
Talk about edge case: empty tree returns empty list
Solution Code
O(n log n) timefrom collections import defaultdict, deque
class Solution:
def verticalTraversal(self, root: Optional[TreeNode]) -> list[list[int]]:
nodes = []
queue = deque([(root, 0, 0)])
while queue:
node, col, row = queue.popleft()
if node:
nodes.append((col, row, node.val))
queue.append((node.left, col - 1, row + 1))
queue.append((node.right, col + 1, row + 1))
nodes.sort()
result = []
current_col = None
for col, row, val in nodes:
if col != current_col:
result.append([])
current_col = col
result[-1].append(val)
return resultFrequently Asked Questions
What is the Vertical Order Traversal of a Binary Tree problem?
Given a binary tree, return the vertical order traversal of its nodes values. For each column from left to right, nodes are ordered by their row (top to bottom), and nodes at the same position are sorted by value. This requires coordinating column, row, and value ordering simultaneously.
How do you solve Vertical Order Traversal of a Binary Tree?
The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Vertical Order Traversal of a Binary Tree?
Vertical Order Traversal of a Binary Tree is asked at Salesforce. It is a hard difficulty problem.
What are common mistakes on Vertical Order Traversal of a Binary Tree?
- Clarify the tie-breaking rule: same column and row means sort by value
- Ask whether it is vertical traversal (column-first) or top-down (row-first)
- Discuss BFS vs DFS: BFS gives natural row ordering, DFS does not
- Mention that column can be negative (left subtree), zero (root), positive (right)
- Talk about edge case: empty tree returns empty list