The problem. Given the root of a binary tree, return its top view: the nodes you would see looking down on the tree from above, from the leftmost column to the rightmost. Give the root column 0; a left child is one column to the left (−1), a right child one column to the right (+1). In each column you see only the highest node. (If two nodes share a column and a level, the one further left in level order counts.)
Input: root = [1, 2, 3, 4, 5, 6, 7] Output: [4, 2, 1, 3, 7]
Input: root = [1, 2, 3, null, 4, null, null, null, 5, null, 6] Output: [2, 1, 3, 6]Each node gets two coordinates: its column (from the left/right moves) and its depth. The top view is, for each column, the node with the smallest depth. The approaches differ in how they find "smallest depth per column" and how they put the columns in order.
Walk the tree and record (column, depth, visit order, value) for every node. Sort by column, then depth, then visit order, and keep the first entry of each column.
function topView(root) { const all = []; // [column, depth, visit order, value] for every node let order = 0; function dfs(node, col, depth) { if (node === null) return; all.push([col, depth, order++, node.val]); dfs(node.left, col - 1, depth + 1); dfs(node.right, col + 1, depth + 1); } dfs(root, 0, 0); all.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]); // by column, then height const view = []; for (let i = 0; i < all.length; i++) { if (i === 0 || all[i][0] !== all[i - 1][0]) view.push(all[i][3]); // first of each column } return view;}Time O(n log n) for the sort, space O(n) — everything is recorded, though only one node per column is wanted.
Instead of recording everything, keep a map from column to the highest node seen so far in it. While walking, replace a column's entry only if the new node is strictly higher (smaller depth). Preorder visits the left side first, so on a tie the leftmost node is already there. At the end, read the columns in order.
function topView(root) { const top = new Map(); // column -> [depth, value] of the highest node so far function dfs(node, col, depth) { if (node === null) return; if (!top.has(col) || depth < top.get(col)[0]) top.set(col, [depth, node.val]); // higher than before? dfs(node.left, col - 1, depth + 1); dfs(node.right, col + 1, depth + 1); } dfs(root, 0, 0); return [...top.keys()].sort((a, b) => a - b).map((col) => top.get(col)[1]);}Time O(n log c) for c columns (the sorted map or the final sort), space O(c + h). The depth comparisons are needed because depth-first search can reach a deep node in a column before a higher one.
Breadth-first search visits nodes top to bottom, left to right. So the first node it meets in a column is automatically the highest (and leftmost on a tie) — no depth to compare. And columns form one unbroken range from minCol to maxCol, so track those two numbers and read the answer column by column — no sorting.
The caption under each node is its column; hidden nodes are greyed out.
Time O(n), space O(n) for the queue and the map — no sorting and no depth bookkeeping. Why are the columns an unbroken range? A node in column c is reached through a parent in column c − 1 or c + 1, so columns spread outward one step at a time from 0.
O(n log n)Every node is recorded, then all n are sorted.
O(n)Every node is stored with its column and depth.
[].[1, 2, null, 3, null, 4]: every node is visible: [4, 3, 2, 1].Was this helpful?
O(n log c)Each node does one lookup in a sorted map of c columns.
O(c + h)One entry per column, plus the recursion.
O(n)Each node is queued once; the first node seen in a column is the highest, so no sorting.
O(n)The queue and the column map.