↓Skip to main content
  1. LeetCode/

LeetCode 314: Binary Tree Vertical Order Traversal

·2 mins· ·
LeetCode Medium Hash-Table Tree Depth-First-Search Breadth-First-Search Sorting Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-10-22

  • Submissions: Accepted ×2

Approach
#

The root is column 0; a left child is column -1 from its parent and a right child +1. Output columns left to right; within a column go top to bottom, and nodes in the same row and column go left to right.

Walk with BFS (Tree Template 2) carrying the column index. BFS already visits top to bottom and left to right within a level, so appending to the column’s list gives the right order. Finally output the columns in sorted order.

My Attempts
#

Accepted on the first try; I submitted the same code twice.

Solution
#

class Solution:
    def verticalOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        cols = defaultdict(list)
        queue = deque([(root, 0)])
        while queue:
            node, col = queue.popleft()
            if node:
                cols[col].append(node.val)
                queue.append((node.left, col - 1))
                queue.append((node.right, col + 1))
        return [cols[c] for c in sorted(cols)]

Complexity
#

Time O(n + k log k) where k is the number of columns; space O(n). Tracking the min and max column and iterating between them removes the sort.

Mistakes to Avoid
#

  • With DFS a deeper node can be visited first, so the order within a column breaks unless the row is stored and sorted.
  • Here nodes in the same row and column go left to right; LC 987 sorts them by value instead.

Interview Explanation
#

I use BFS and carry each node’s column: left child minus one, right child plus one. BFS visits top to bottom and left to right within a level, so appending to each column’s list already matches the required order. Then I output the columns from smallest to largest: O(n + k log k), or O(n) if I track the min and max column.

Related

LeetCode 117: Populating Next Right Pointers in Each Node II
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 117 note: connect next pointers level by level with BFS; for a non-perfect tree, looking only at the neighbour’s children breaks the chain
LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum
LeetCode 199: Binary Tree Right Side View
·2 mins
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 199 note: in a level-order traversal, each level’s rightmost node is what you see from the right
LeetCode 113: Path Sum II
·2 mins
LeetCode Medium Backtracking Tree Depth-First-Search Binary-Tree
LeetCode 113 note: backtrack with the current path and store a copy at a leaf whose total matches; values can be negative, so no early pruning
LeetCode 116: Populating Next Right Pointers in Each Node
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search
LeetCode 116 note: in a perfect binary tree, walk each level through the next pointers already set on the level above: left.next = right and right.next = next.left, with O(1) extra space
LeetCode 129: Sum Root to Leaf Numbers
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Tree
LeetCode 129 note: on the way down multiply the current number by 10 and add the node’s value, adding it to the total at a leaf