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 is assigned 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
Which data structure is commonly used to find the top view of a binary tree?
A queue is used for level order traversal, and a map (or dictionary) is used to store the first node at each horizontal distance.
Click to reveal answer
intermediate
Why do we store only the first node encountered at each horizontal distance during traversal?
Because the first node encountered at a horizontal distance is the topmost node visible from above. Later nodes at the same horizontal distance are hidden behind it.
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
What horizontal distance is assigned to the root node in top view calculation?
ADepends on tree height
B1
C-1
D0
✗ Incorrect
The root node is always assigned horizontal distance 0.
Which traversal method is best suited to find the top view of a binary tree?
ALevel order traversal
BPreorder traversal
CInorder traversal
DPostorder traversal
✗ Incorrect
Level order traversal visits nodes level by level, which helps identify the first node at each horizontal distance.
If two nodes share the same horizontal distance, which one appears in the top view?
AThe one at the higher level
BThe one at the lower level
CThe rightmost node
DThe leftmost node
✗ Incorrect
The node at the higher level (closer to root) is visible from the top.
What data structure helps keep track of nodes and their horizontal distances during traversal?
AStack
BSet
CQueue
DLinked list
✗ Incorrect
A queue is used for level order traversal to process nodes in order.
What is the space complexity of storing the top view nodes in a map?
AO(1)
BO(n)
CO(log n)
DO(n^2)
✗ Incorrect
In the worst case, the map stores one node per horizontal distance, which can be up to O(n).
Explain how to find the top view of a binary tree step-by-step.
Think about how you see the tree from above and which nodes block others.
You got /5 concepts.
Why is level order traversal preferred over inorder or preorder for top view?
Consider which traversal sees nodes closest to root first.
You got /4 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 top
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. All leaf nodes of the tree
Solution
Step 1: Understand the meaning of top view
The top view of a binary tree shows nodes visible when looking down vertically from above the tree.
Step 2: Compare with other views
Left or right views show nodes from side angles, and leaf nodes are only the bottom nodes, not the top view.
Final Answer:
Nodes visible when the tree is viewed from the top -> Option A
Quick Check:
Top view = nodes seen from above [OK]
Hint: Top view shows nodes visible from above the tree [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 data structure is commonly used to find the top view of a binary tree in C?
easy
A. Stack
B. Queue
C. Hash Map
D. Linked List
Solution
Step 1: Identify traversal method for top view
Top view is found using level order traversal, which uses a queue to process nodes level by level.
Step 2: Understand role of queue
Queue helps visit nodes in order from top to bottom and left to right, needed for correct top view.
Final Answer:
Queue -> Option B
Quick Check:
Level order traversal uses queue [OK]
Hint: Use queue for level order traversal in top view [OK]
Common Mistakes:
Using stack which is for depth-first traversal
Confusing hash map as traversal structure
Using linked list instead of queue
3. What is the output top view of the binary tree below?
1
/ \
2 3
\ \
4 5
\ \
6 7
medium
A. [1, 2, 3, 4, 5]
B. [2, 1, 4, 6, 7]
C. [2, 1, 3, 4, 7]
D. [2, 1, 3, 5, 7]
Solution
Step 1: Assign horizontal distances
Assign horizontal distance (HD) from root: root(1) = 0, left child decreases HD by 1, right child increases HD by 1. So, 2 = -1, 3 = 1, 4 = 0, 5 = 2, 6 = 1, 7 = 3.
Step 2: Select topmost nodes at each HD
At HD -1: 2; HD 0: 1 (4 is lower); HD 1: 3 (6 is lower); HD 2: 5; HD 3: 7.
Final Answer:
[2, 1, 3, 5, 7] -> Option D
Quick Check:
Top view nodes by HD = [2,1,3,5,7] [OK]
Hint: Use horizontal distance and pick top nodes [OK]
Common Mistakes:
Choosing lower nodes instead of top nodes
Ignoring horizontal distance concept
Mixing order of nodes in output
4. Consider this C code snippet to print top view of a binary tree:
void topView(Node* root) {
if (!root) return;
map topNode;
queue> q;
q.push({root, 0});
while (!q.empty()) {
auto p = q.front(); q.pop();
Node* node = p.first;
int hd = p.second;
if (topNode.find(hd) == topNode.end())
topNode[hd] = node->data;
if (node->left) q.push({node->left, hd - 1});
if (node->right) q.push({node->right, hd + 1});
}
for (auto it : topNode)
cout << it.second << " ";
}
What is the error in this code?
medium
A. Incorrect horizontal distance calculation
B. Not checking if root is NULL
C. Using C++ STL map and queue in C code
D. Missing return statement in void function
Solution
Step 1: Identify language mismatch
The code uses C++ STL containers like map and queue, but the function is declared in C style which does not support these STL features.
Step 2: Check other code parts
Root NULL check is present, horizontal distance is correctly updated, and void functions do not require return statements.
Final Answer:
Using C++ STL map and queue in C code -> Option C
Quick Check:
C code cannot use C++ STL containers [OK]
Hint: C code can't use C++ STL containers like map or queue [OK]
Common Mistakes:
Ignoring language differences between C and C++
Thinking void functions need return
Assuming horizontal distance logic is wrong
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 leftmost node at that horizontal distance?
hard
A. Store the first node encountered at each horizontal distance during level order traversal
B. Store the last node encountered at each horizontal distance during level order traversal
C. Store nodes with maximum depth at each horizontal distance
D. Store nodes with minimum depth at each horizontal distance but prefer right child first
Solution
Step 1: Understand node selection in top view
Top view shows the first node encountered at each horizontal distance during level order traversal, which naturally picks the leftmost node at that distance.
Step 2: Avoid overwriting nodes
By storing only the first node at each horizontal distance and ignoring later nodes, the leftmost node is preserved.
Final Answer:
Store the first node encountered at each horizontal distance during level order traversal -> Option A
Quick Check:
First node at HD = leftmost node [OK]
Hint: Keep first node per horizontal distance in level order [OK]
Common Mistakes:
Overwriting with last node instead of first
Choosing nodes by max depth instead of min
Preferring right child first breaks leftmost rule
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.