DSA Tracker

Medium

Top View of Binary Tree

A medium Binary Trees problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Binary Trees
Sheets
2
Core for
4 roles
Platform
GeeksforGeeks

The problem

Given the root of a binary tree, return the values of the nodes visible from the top when looking at the tree from above. A node is visible from the top if no node above it in the same vertical column has a smaller row.

Example 1

Input
[1,2,3,4,5,6,7]
Output
[4,2,1,3,7]
Why
Looking from above, the topmost node in each vertical column is visible: 4 at column -2, 2 at -1, 1 at 0, 3 at 1, 7 at 2.

Example 2

Input
[1,2,3,null,5,null,7]
Output
[2,1,3,7]
Why
Nodes visible from top: 2 at column -1, 1 at column 0, 3 at column 1, 7 at column 2.

Example 3

Input
[1]
Output
[1]
Why
The single root node is visible.

Constraints

  • The number of nodes is in the range [1, 1000].
  • -1000 <= Node.val <= 1000

How to think about it

Updated 2026-09-09

The top view contains exactly one node per vertical column: the first node encountered at that horizontal distance. Breadth-first search visits nodes level by level from top to bottom, which means the very first time a column coordinate is visited in BFS, that node is guaranteed to be the top-view node.

Approaches, worst first

  1. DFS with depth map

    time O(n log n) · space O(n)

    Pass `(col, depth)` recursively. In a map of `col -> (val, depth)`, update an entry only if the current depth is strictly smaller than the existing recorded depth. Extract columns in sorted order after tree traversal completes.

  2. BFS with first-arrival recordingWrite this one

    time O(n) · space O(n)

    Queue tuples of `(node, col)`. Keep a hash map of column to value. Because BFS descends row by row, if `map` does not contain `col`, record `node.val`. Track min and max column to emit the final array directly without sorting keys.

Where people lose marks · 3
  • Using DFS and blindly recording the first visited node for a column without comparing depths; DFS dives deep along the left before visiting shallower nodes in other branches.
  • Overwriting earlier recorded entries in BFS; subsequent arrivals on the same column are deeper and therefore obscured from above.
  • Sorting map keys at the end when tracking `minCol` and `maxCol` allows O(n) array assembly.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

What each operation costs

OperationTime
traverse all nodes using recursion or queueO(n)
search for an arbitrary value in an unordered treeO(n)
call stack memory on balanced treeO(log n)
call stack memory on skewed treeO(n)
What usually goes wrong with Binary Trees
  • Dereferencing left or right child pointers without checking if the current node is null, throwing null pointer errors on empty trees or leaf nodes.
  • Defining a leaf node incorrectly by stopping when either child is null instead of checking that both left and right children are simultaneously null.
  • Computing tree diameter by taking left height plus right height inside a recursive helper without updating a global maximum across every visited node.

Which roles need this problem

Binary Trees is a core topic for these 4 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including Full-Stack Developer, Android Developer, iOS Developer.

Companies that have asked it

Tags taken from the problem's own GeeksforGeeks page — not a copied list.

NPCIOla CabsPaytmWalmart

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Binary Trees problems

Problem set and role mapping as of .