Problem
Section titled “Problem”Given a binary tree, the task is to return its top view. The top view of a binary tree is the set of nodes visible when the tree is viewed from the top.
- Return the nodes from the leftmost node to the rightmost node.
- If multiple nodes overlap at the same horizontal position, only the topmost (closest to the root) node is included in the view.
Complete the function topView(), which takes the root of the tree and returns the list of node values in the top view.
Example 1:

Input: root = [1, 2, 3]Output: [2, 1, 3]Explanation: The green coloured nodes represent the top view in the binary tree above.Example 2:

Input: root = [10, 20, 30, 40, 60, 90, 100]Output: [40, 20, 10, 30, 100]Explanation: The green coloured nodes represent the top view in the binary tree above.Constraints:
- number of nodes
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Give every node a horizontal distance (column): the root is column 0, a left child is one column left of its parent (col - 1) and a right child one column right (col + 1). Looking from above, each column shows exactly one node — the one closest to the root in that column.
A level-order traversal (BFS) visits nodes in increasing depth, and within one depth from left to right. So the first node BFS reaches in a column is the topmost one (and among equally deep nodes, the leftmost, which is the convention the judge uses). We just record a column’s value the first time we see it and ignore later nodes.
Columns are contiguous and lie in [-(n - 1), n - 1], so instead of an ordered map we can store them in an array top indexed by col + n and remember the smallest and largest column seen; reading top[minCol + n .. maxCol + n] gives the answer in left-to-right order without sorting.
A depth-first version needs a depth comparison per column to be correct, and on a skewed tree of nodes its recursion can overflow the stack; BFS has neither problem.
Algorithm
Section titled “Algorithm”- Let
nbe the number of nodes (counted first with the same BFS, or any upper bound). Createtopof size2n + 1, filled with-1(values are positive, so-1means “column not seen yet”), and setminCol = maxCol = 0. - BFS from
(root, 0)with a queue of(node, col)pairs. For each popped pair:- if
top[col + n] == -1, set it tonode->dataand updateminCol/maxCol; - push
(node->left, col - 1)and(node->right, col + 1)for the children that exist.
- if
- Return
top[minCol + n], ..., top[maxCol + n].
Walkthrough
Section titled “Walkthrough”Example 2 with each node’s column. BFS reaches 10 (col 0), 20 (col -1), 30 (col 1), 40 (col -2), 60 (col 0, already taken), 90 (col 0, taken), 100 (col 2). The green nodes are the first in their column:
Reading columns -2 .. 2 gives [40, 20, 10, 30, 100].
Complexity
Section titled “Complexity”- Time: — each node is enqueued and dequeued once (twice counting the size pass), and the output scan covers at most
2n + 1slots. - Space: — the queue and the
toparray.
Edge cases
Section titled “Edge cases”- Skewed tree (a chain of left children): every node opens a new column; BFS uses no recursion, so there is no stack overflow.
- Overlapping nodes deeper down (
60and90in Example 2 share column0with the root): BFS reaches the root first, so the deeper nodes are ignored. - Single node: the answer is
[root->data].
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: vector<int> topView(Node *root) { if (!root) return {};
int n = 0; // number of nodes: columns lie in [-(n - 1), n - 1] queue<Node *> nodes; nodes.push(root); while (!nodes.empty()) { Node *cur = nodes.front(); nodes.pop(); n++; if (cur->left) nodes.push(cur->left); if (cur->right) nodes.push(cur->right); }
vector<int> top(2 * n + 1, -1); // top[col + n] = first value seen in that column int minCol = 0, maxCol = 0; queue<pair<Node *, int>> q; q.push({root, 0}); while (!q.empty()) { auto [cur, col] = q.front(); q.pop(); if (top[col + n] == -1) { top[col + n] = cur->data; minCol = min(minCol, col); maxCol = max(maxCol, col); } if (cur->left) q.push({cur->left, col - 1}); if (cur->right) q.push({cur->right, col + 1}); }
return vector<int>(top.begin() + minCol + n, top.begin() + maxCol + n + 1); }};class Solution { public ArrayList<Integer> topView(Node root) { ArrayList<Integer> ans = new ArrayList<>(); if (root == null) return ans;
int n = 0; // number of nodes: columns lie in [-(n - 1), n - 1] ArrayDeque<Node> nodes = new ArrayDeque<>(); nodes.offer(root); while (!nodes.isEmpty()) { Node cur = nodes.poll(); n++; if (cur.left != null) nodes.offer(cur.left); if (cur.right != null) nodes.offer(cur.right); }
int[] top = new int[2 * n + 1]; // top[col + n] = first value seen in that column Arrays.fill(top, -1); int minCol = 0, maxCol = 0; ArrayDeque<Node> q = new ArrayDeque<>(); ArrayDeque<Integer> cols = new ArrayDeque<>(); // column of the matching node in q q.offer(root); cols.offer(0); while (!q.isEmpty()) { Node cur = q.poll(); int col = cols.poll(); if (top[col + n] == -1) { top[col + n] = cur.data; minCol = Math.min(minCol, col); maxCol = Math.max(maxCol, col); } if (cur.left != null) { q.offer(cur.left); cols.offer(col - 1); } if (cur.right != null) { q.offer(cur.right); cols.offer(col + 1); } }
for (int c = minCol; c <= maxCol; c++) ans.add(top[c + n]); return ans; }}from collections import deque
class Solution: def topView(self, root): if not root: return []
n = 0 # number of nodes: columns lie in [-(n - 1), n - 1] nodes = deque([root]) while nodes: cur = nodes.popleft() n += 1 if cur.left: nodes.append(cur.left) if cur.right: nodes.append(cur.right)
top = [-1] * (2 * n + 1) # top[col + n] = first value seen in that column min_col = max_col = 0 q = deque([(root, 0)]) while q: cur, col = q.popleft() if top[col + n] == -1: top[col + n] = cur.data min_col = min(min_col, col) max_col = max(max_col, col) if cur.left: q.append((cur.left, col - 1)) if cur.right: q.append((cur.right, col + 1))
return top[min_col + n:max_col + n + 1]Give it a try first
Attempt the problem on GeeksforGeeks — the code will still be here.
