What does the Top View of a binary tree represent?
The top view of a binary tree shows the nodes visible when the tree is seen from above. It includes the nodes that appear 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 for the top view?
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 algorithm to find the top view of a binary tree?
The queue helps in level order traversal (breadth-first), ensuring nodes are processed top-down. The map stores the first node encountered at each horizontal distance, which forms the top view.
Click to reveal answer
intermediate
What is the time complexity of finding the top view of a binary tree using level order traversal?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the 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 hidden behind.
Click to reveal answer
What is the horizontal distance of the root node in a binary tree?
A1
B0
C-1
DDepends on the tree height
✗ Incorrect
The root node is assigned a horizontal distance of 0 by definition.
Which traversal method is best suited to find the top view of a binary tree?
ALevel order traversal
BPreorder traversal
CPostorder traversal
DInorder traversal
✗ Incorrect
Level order traversal processes nodes level by level, which helps identify the topmost nodes at each horizontal distance.
If two nodes have the same horizontal distance, which one appears in the top view?
AThe node at the lower level
BThe right child node
CBoth nodes appear
DThe node at the higher level
✗ Incorrect
The node at the higher (closer to root) level is visible from the top, so it appears in the top view.
What data structure is used to store the first node at each horizontal distance?
AStack
BQueue
CMap or Dictionary
DLinked List
✗ Incorrect
A map or dictionary stores nodes keyed by their horizontal distance to track the first node encountered.
What is the output of the top view for a single-node binary tree?
AThe single node itself
BEmpty
CNull
DDepends on the node value
✗ Incorrect
With only one node, the top view is just that node.
Explain how to compute the top view of a binary tree step-by-step.
Think about how to visit nodes and remember which ones are visible from the top.
You got /5 concepts.
Describe why level order traversal is preferred over inorder or preorder for the top view problem.
Consider the order in which nodes appear from top to bottom.
You got /4 concepts.
Practice
(1/5)
1. What does the top view of a binary tree represent?
easy
A. All leaf nodes of the tree
B. Nodes visible when the tree is viewed from the left side
C. Nodes visible when the tree is viewed from the right side
D. Nodes visible when the tree is viewed from above
Solution
Step 1: Understand the top view concept
The top view of a binary tree shows nodes visible from above, ignoring nodes hidden behind others.
Step 2: Compare with other views
Left or right views show nodes from sides, and leaf nodes are only the bottom nodes.
Final Answer:
Nodes visible when the tree is viewed from above -> Option D
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 JavaScript data structure is best suited to store nodes by their horizontal distance when computing the top view of a binary tree?
easy
A. Set
B. Array
C. Map
D. Stack
Solution
Step 1: Identify the need for key-value pairs
We need to store nodes with their horizontal distance as keys and node values as values.
Step 2: Choose the best data structure
A Map allows storing key-value pairs with efficient lookup and insertion.
Final Answer:
Map -> Option C
Quick Check:
Map stores horizontal distance keys [OK]
Hint: Use Map for key-value horizontal distance storage [OK]
Common Mistakes:
Using Array which is index-based, not key-based
Using Set which stores only unique values without keys
Using Stack which is LIFO, not suitable here
3. What is the output of the following code snippet that prints the top view of the binary tree?
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function topView(root) {
if (!root) return [];
const map = new Map();
const queue = [{ node: root, hd: 0 }];
while (queue.length) {
const { node, hd } = queue.shift();
if (!map.has(hd)) map.set(hd, node.data);
if (node.left) queue.push({ node: node.left, hd: hd - 1 });
if (node.right) queue.push({ node: node.right, hd: hd + 1 });
}
return [...map.entries()]
.sort((a, b) => a[0] - b[0])
.map(entry => entry[1]);
}
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(4);
root.right.right = new Node(5);
console.log(topView(root));
medium
A. [2, 1, 3, 5]
B. [2, 1, 3]
C. [2, 1, 4, 3, 5]
D. [1, 2, 3, 4, 5]
Solution
Step 1: Traverse the tree with horizontal distances
Start at root (1) with hd=0, add left child (2) hd=-1, right child (3) hd=1, then 4 at hd=0 (ignored since hd=0 exists), 5 at hd=2.
Step 2: Collect first nodes at each horizontal distance
Hint: First node at each horizontal distance is in top view [OK]
Common Mistakes:
Including nodes hidden behind others at same hd
Not sorting by horizontal distance before output
Adding nodes multiple times for same hd
4. Identify the bug in this top view function and select the correct fix:
function topView(root) {
if (!root) return [];
const map = new Map();
const queue = [{ node: root, hd: 0 }];
while (queue.length) {
const { node, hd } = queue.pop();
if (!map.has(hd)) map.set(hd, node.data);
if (node.left) queue.push({ node: node.left, hd: hd - 1 });
if (node.right) queue.push({ node: node.right, hd: hd + 1 });
}
return [...map.entries()]
.sort((a, b) => a[0] - b[0])
.map(entry => entry[1]);
}
medium
A. Replace queue.pop() with queue.shift() to process nodes in correct order
B. Change map to a Set to avoid duplicate nodes
C. Remove sorting before returning the result
D. Add a visited set to avoid infinite loops
Solution
Step 1: Understand queue usage in BFS
Using queue.pop() removes last element, making it a stack (DFS), not BFS. BFS requires queue.shift() to process nodes in FIFO order.
Step 2: Fix the traversal order
Replacing pop() with shift() ensures nodes are processed level by level, correctly capturing top view nodes.
Final Answer:
Replace queue.pop() with queue.shift() to process nodes in correct order -> Option A
Quick Check:
BFS needs shift(), not pop() [OK]
Hint: Use shift() for BFS queue, pop() is for stack [OK]
Common Mistakes:
Using pop() causing wrong traversal order
Using Set instead of Map losing key-value pairs
Skipping sorting which orders output incorrectly
5. Given a binary tree where multiple nodes share the same horizontal distance and level, how should the top view algorithm decide which node to include in the output?
hard
A. Include the node that appears last in level order traversal at that horizontal distance
B. Include the node that appears first in level order traversal at that horizontal distance
C. Include the node with the maximum value at that horizontal distance
D. Include all nodes at that horizontal distance
Solution
Step 1: Understand node conflicts at same horizontal distance and level
When nodes share horizontal distance and level, top view includes the first encountered node in level order (BFS) traversal.
Step 2: Reason why first node is chosen
Because BFS visits nodes level by level from left to right, the first node at that horizontal distance is visible from top.
Final Answer:
Include the node that appears first in level order traversal at that horizontal distance -> Option B
Quick Check:
Top view = first node at hd in BFS order [OK]
Hint: Pick first node at horizontal distance in BFS [OK]
Common Mistakes:
Choosing node by max value instead of order
Including all nodes causing duplicates
Choosing last node instead of first
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.