ProgramGuru
| Input Tree | Top View Output | Description |
|---|---|---|
| [1, 2, 3, 4, 5, null, 6]
|
[4, 2, 1, 3, 6] | Standard tree with multiple levels; leftmost and rightmost nodes at each level appear in top view |
| [1]
|
[1] | Single node tree; root itself is the top view |
| [] | [] | Empty tree with no nodes returns an empty top view |
| [1, 2, null, 3, null, 4]
|
[4, 3, 2, 1] | Left-skewed tree; only the leftmost path visible in top view |
| [1, null, 2, null, 3, null, 4]
|
[1, 2, 3, 4] | Right-skewed tree; every node appears in top view due to unique horizontal distances |
| [1, 2, 3, 4, null, null, 5, 6]
|
[6, 4, 2, 1, 3, 5] | Complex tree with deep left and right subtrees; top view includes the outermost nodes |
The Top View of a binary tree refers to the set of nodes visible when we look at the tree from the top. For each vertical column (based on horizontal distance from the root), we should see only the topmost node — the one that appears first in a top-down traversal.
We’ll solve this using a level-order traversal (BFS) approach, and for each horizontal distance (HD), we record the first node we encounter. This way, we capture the top view from left to right.
Consider the binary tree:
1
/ 2 3
/ 4 5 6
Here, node 1 is the root. Node 2 is on the left, node 3 is on the right. Node 4 is the right child of 2, and nodes 5 and 6 are the children of 3.
Assign a horizontal distance (HD) to each node:
So we get:
Use a queue for BFS. Track both node and its HD. Maintain a map where key = HD and value = first node seen at that HD.
Processing in BFS order (top to bottom, left to right):
The map now has nodes at each horizontal distance:
Sort keys: [-1, 0, 1, 2], then print values in that order: [2, 1, 3, 6]
If the tree is empty (root = null), then the top view is simply an empty list.
If the tree has only one node (the root), then the top view contains just that one node.
Each node only has a left child. Every node will have a unique HD (all negative) and will be visible from the top.
Each node only has a right child. Each node gets a unique positive HD and will appear in the top view.
In trees where multiple nodes share the same HD, only the first node encountered in level-order traversal is taken for the top view. This ensures higher nodes (closer to root) are prioritized.
The top view of a binary tree helps us understand its vertical structure from above. By tracking horizontal distances during a BFS traversal, we efficiently extract the topmost visible node at each vertical level. Always sort the HDs before output to ensure left-to-right ordering.
This method ensures correctness for all types of binary trees — from skewed to balanced and general trees with complex structures.
queue and enqueue a tuple containing the root node and its horizontal distance (0).node, hd).hd is not present in the map, record the node's value for that hd.hd - 1 and the right child with hd + 1 (if they exist).#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello, World from C!\n");
return 0;
}
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗