The top view of a binary tree is the set of nodes visible when the tree is seen from above. It includes the nodes that are the first at each horizontal distance from the root.
Click to reveal answer
beginner
How do we determine the horizontal distance (HD) of nodes in a binary tree?
The root node has HD = 0. For any node, its left child has HD = parent's HD - 1, and its right child has HD = parent's HD + 1.
Click to reveal answer
intermediate
Why do we use a queue and a map/dictionary in the top view algorithm?
A queue helps in level order traversal (breadth-first), and a map stores the first node encountered at each horizontal distance to ensure only the topmost node is recorded.
Click to reveal answer
intermediate
What is the time complexity of finding the top view of a binary tree?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the level order traversal.
Click to reveal answer
intermediate
In the top view, what happens if two nodes share the same horizontal distance and level?
Only the first node encountered at that horizontal distance during level order traversal is included in the top view. Nodes at the same HD but lower levels are ignored.
Click to reveal answer
What data structure is commonly used to perform the traversal for top view of a binary tree?
AStack
BQueue
CPriority Queue
DLinked List
✗ Incorrect
A queue is used for level order traversal which is essential to find the top view.
What is the horizontal distance of the root node in a binary tree?
A0
B1
C-1
DDepends on tree height
✗ Incorrect
The root node is assigned a horizontal distance of 0.
If two nodes have the same horizontal distance, which one appears in the top view?
AThe leftmost node
BThe node at the lower level
CThe node at the higher level (closer to root)
DThe rightmost node
✗ Incorrect
The top view includes the node at the higher level for each horizontal distance.
Which data structure is used to store nodes by their horizontal distance in the top view algorithm?
AStack
BArray
CSet
DMap or Dictionary
✗ Incorrect
A map/dictionary stores the first node encountered at each horizontal distance.
What is the overall time complexity of the top view algorithm for a binary tree with n nodes?
AO(n)
BO(n log n)
CO(n^2)
DO(log n)
✗ Incorrect
Each node is visited once, so the time complexity is O(n).
Explain how to find the top view of a binary tree step-by-step.
Think about how to see the tree from above and which nodes block others.
You got /5 concepts.
Describe the role of horizontal distance in the top view of a binary tree.
Imagine standing above the tree and looking down.
You got /5 concepts.
Practice
(1/5)
1. What does the top view of a binary tree represent?
easy
A. Nodes visible when the tree is viewed from the right side
B. Nodes visible when the tree is viewed from the left side
C. Nodes visible when the tree is viewed from above
D. All leaf nodes of the tree
Solution
Step 1: Understand the top view concept
The top view of a binary tree shows nodes visible from the top, ignoring nodes hidden behind others vertically.
Step 2: Compare with other views
Left or right views show nodes from sides, and leaf nodes are only the bottom-most nodes, so they differ from top view.
Final Answer:
Nodes visible when the tree is viewed from above -> Option C
Quick Check:
Top view = nodes seen from above [OK]
Hint: Top view shows nodes visible from above only [OK]
Common Mistakes:
Confusing top view with left or right view
Thinking top view includes all leaf nodes
Assuming top view shows all nodes
2. Which TypeScript data structure is best suited to store nodes with their horizontal distances while computing the top view of a binary tree?
easy
A. Map (key-value pairs)
B. Array
C. Set
D. Stack
Solution
Step 1: Identify the need for key-value storage
We need to store nodes with their horizontal distances as keys and node values as values.
Step 2: Choose the appropriate data structure
A Map allows storing keys (horizontal distances) with corresponding node values efficiently.
Final Answer:
Map (key-value pairs) -> Option A
Quick Check:
Use Map for horizontal distance to node mapping [OK]
Hint: Use Map to link horizontal distance and node [OK]
Common Mistakes:
Using Array which is index-based, not key-based
Using Set which stores unique values without keys
Using Stack which is LIFO, not suitable here
3. Given the following TypeScript code snippet for top view, what will be the printed output?
class Node {
constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {}
}
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
root.left.right.right.right = new Node(6);
function topView(root: Node) {
const map = new Map();
const queue: Array<[Node, number]> = [[root, 0]];
while (queue.length) {
const [node, hd] = queue.shift()!;
if (!map.has(hd)) {
map.set(hd, node.data);
}
if (node.left) queue.push([node.left, hd - 1]);
if (node.right) queue.push([node.right, hd + 1]);
}
const sortedKeys = Array.from(map.keys()).sort((a, b) => a - b);
const result = sortedKeys.map(key => map.get(key));
console.log(result);
}
topView(root);
medium
A. [1, 2, 3, 4, 5, 6]
B. [2, 4, 1, 3, 6]
C. [2, 1, 3]
D. [2, 1, 3, 6]
Solution
Step 1: Assign horizontal distances and track first nodes
Root(1) at hd=0, left(2) at hd=-1, right(3) at hd=1. Next nodes 4 at hd=0 (ignored, already 1), 5 at hd=1 (ignored, already 3), 6 at hd=2 (new).
Step 2: Collect nodes in order of horizontal distance
Hint: First node at each horizontal distance is in top view [OK]
Common Mistakes:
Including nodes hidden behind others at same horizontal distance
Not sorting horizontal distances before output
Adding all nodes instead of first at each horizontal distance
4. Identify the bug in the following TypeScript code for top view of a binary tree:
function topView(root: Node) {
const map = new Map();
const queue: Array<[Node, number]> = [[root, 0]];
while (queue.length) {
const [node, hd] = queue.pop()!;
if (!map.has(hd)) {
map.set(hd, node.data);
}
if (node.left) queue.push([node.left, hd - 1]);
if (node.right) queue.push([node.right, hd + 1]);
}
const sortedKeys = Array.from(map.keys()).sort((a, b) => a - b);
const result = sortedKeys.map(key => map.get(key));
console.log(result);
}
medium
A. Using queue.pop() instead of queue.shift() causes wrong traversal order
B. Not checking if root is null before processing
C. Not sorting the keys before printing result
D. Using Map instead of Set for horizontal distances
Solution
Step 1: Analyze queue operation impact
Using pop() removes from end, making traversal depth-first instead of breadth-first, breaking horizontal distance order.
Step 2: Correct traversal method
Using shift() removes from front, ensuring level order traversal needed for correct top view.
Final Answer:
Using queue.pop() instead of queue.shift() causes wrong traversal order -> Option A
Quick Check:
Use shift() for BFS queue [OK]
Hint: Use shift() for BFS queue, not pop() [OK]
Common Mistakes:
Using pop() causing DFS instead of BFS
Ignoring null root check (less critical here)
Not sorting keys before output (code does sort)
5. You have a binary tree where multiple nodes share the same horizontal distance and level. How should you modify the top view algorithm to always show the node with the smallest value at each horizontal distance?
hard
A. Store all nodes at each horizontal distance and pick the last inserted node
B. Update the map only if the new node's value is smaller than the existing one at that horizontal distance
C. Ignore horizontal distance and print nodes in level order
D. Use a stack instead of a queue for traversal
Solution
Step 1: Understand the problem with duplicates
When nodes share horizontal distance and level, default top view picks first encountered node, not necessarily smallest value.
Step 2: Modify map update condition
Update the map only if the new node's value is smaller than the stored value for that horizontal distance to ensure smallest value is shown.
Final Answer:
Update the map only if the new node's value is smaller than the existing one at that horizontal distance -> Option B
Quick Check:
Keep smallest node value per horizontal distance [OK]
Hint: Update map only if new node value is smaller [OK]
Common Mistakes:
Ignoring value comparison, keeping first node only
Using stack breaks level order needed
Storing all nodes wastes memory and complicates logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.