Vertical Order Traversal
Vertical Order Traversal of a Binary Tree is LeetCode 987 (Hard). Every node gets a position (row, col):
- The root is at
(0, 0). - A node at
(row, col)has its left child at(row + 1, col - 1)and its right child at(row + 1, col + 1).
Return one list per column, from the leftmost column to the rightmost. Inside a column, list the nodes from top to bottom (increasing row). When two or more nodes share both the same row and the same column, list them in increasing order of value.
The tree has up to 1000 nodes, so collecting every node and sorting is comfortably fast.
- The number of nodes in the tree is in the range [1, 1000].
- 0 <= Node.val <= 1000
Intuition
Draw the tree on graph paper and every node gets a cell (row, col). The required output is those nodes read by column, then by row, then by value, so the problem is really a sort in disguise.
Collect (row, value) for each column in one traversal of any kind, then sort each column. The sort alone decides the order, so it does not matter that a DFS may reach a deep node before a shallow one, or which of two nodes in the same cell it meets first.
When an output order is defined by coordinates you can compute per node (column, depth, distance), give every node its coordinate in one traversal and then sort or bucket by the key. Top view, bottom view and diagonal traversal of a binary tree all follow the same plan with a different rule for what each bucket keeps.
Approach
Before reading on, draw [1,2,3,4,6,5,7] with coordinates and find the cell that holds two nodes. Then decide which sort key puts that column in the right order no matter which traversal you use.
Give every node its coordinates
Run dfs(node, row, col) from dfs(root, 0, 0). The left call gets (row + 1, col - 1) and the right call (row + 1, col + 1). BFS works equally well; only the coordinates matter.
Bucket by column
Append (row, node.val) to cols[col], a hash map (or sorted map) from column to a list of pairs. Storing the row next to the value is what lets the sort fix the order later.
Sort each column and read left to right
Go through the columns in increasing order. Sort each list of pairs: tuples compare by row first and by value on a tie, which is exactly the rule. Keep only the values.
Why the order is right
Every node lands in exactly one bucket, so nothing is lost or repeated. Columns are emitted from smallest to largest, and within a column the tuple sort orders by row and then by value, which is the problem's full ordering. The traversal's own visiting order never reaches the output.
Code in Python, C++ and Java
Common pitfalls
Keeping traversal order on a tie
cols[col].append(node.val) ... res.append(cols[col])
cols[col].append((row, node.val)) ... res.append([v for _, v in sorted(cols[col])])
In [1,2,3,4,6,5,7], 6 (from the left subtree) and 5 (from the right) both sit at row 2, column 0. Any left-first traversal meets 6 first, but the expected column is [1,5,6].
Sorting by value only
sorted(v for _, v in cols[col])
[v for _, v in sorted(cols[col])]
Values decide only ties within the same row. In a column holding the root 9 and a deeper node 2, sorting by value alone puts 2 above 9, which reverses the top-to-bottom order.
Trusting DFS order for rows
# DFS visits top to bottom, so rows are already sorted
sort by (row, value) inside each column
DFS finishes the whole left subtree before the right one. In [1,2,3,null,4,null,null,null,6] it reaches 6 at row 3 of column 1 before 3 at row 1 of the same column. BFS does give rows in order, but it still needs the value tie-break.
Complexity
LeetCode 987 vs LeetCode 314 vs the GFG version
Three problems share the name binary tree vertical order traversal. They agree on columns and differ only on ties.
| Problem | Same row and column | Usual approach |
|---|---|---|
| LeetCode 987, Vertical Order Traversal of a Binary Tree | smaller value first | collect (col, row, value), sort |
| LeetCode 314, Binary Tree Vertical Order Traversal | left to right, as BFS meets them | BFS, append to column lists, no sort |
| GFG, Vertical Traversal of Binary Tree | level order, like 314 | BFS with a map from column to list |
Vertical Order Traversal FAQ
What is the whole method in four lines?
- Coordinates: root at
(0, 0); left child(row + 1, col - 1); right child(row + 1, col + 1). - Collect: one DFS or BFS, appending
(row, value)to the list forcol. - Output: columns in increasing order; each column sorted by row, then value.
- Complexity: O(n log n) time, O(n) space.
Can BFS be used instead of DFS?
Yes. A queue of (node, row, col) visits rows in order, so each column list is already sorted by row. For LeetCode 987 you still have to sort nodes that share a row and column by value; for LeetCode 314 and the GFG version, BFS order alone is the answer.
Why is LeetCode 987 marked Hard?
Not for the algorithm, which is a traversal and a sort. It is the precise ordering rule: the row must be part of the sort for DFS, and nodes in the same cell must be ordered by value rather than by visiting order. Most wrong answers come from one of those two.