The bottom view of a binary tree is the set of nodes visible when the tree is seen from the bottom. It shows the last node at each horizontal distance from the root.
Click to reveal answer
beginner
How do we assign horizontal distances to nodes in a binary tree for bottom view?
We assign horizontal distance (HD) starting with root as 0. Left child has HD = parent HD - 1, right child has HD = parent HD + 1.
Click to reveal answer
intermediate
Which data structure is commonly used to track nodes and their horizontal distances in bottom view?
A map or dictionary is used to store the latest node at each horizontal distance. A queue is used for level order traversal.
Click to reveal answer
intermediate
In bottom view, if multiple nodes share the same horizontal distance, which node is visible?
The node that appears last in level order traversal (lowest level) at that horizontal distance is visible in the bottom view.
Click to reveal answer
intermediate
What is the time complexity of computing the bottom 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 level order traversal.
Click to reveal answer
What horizontal distance is assigned to the root node in bottom view calculation?
A1
B0
C-1
DDepends on tree height
✗ Incorrect
The root node is always assigned horizontal distance 0.
Which traversal method is best suited to compute the bottom view of a binary tree?
ALevel order traversal
BInorder traversal
CPostorder traversal
DPreorder traversal
✗ Incorrect
Level order traversal visits nodes level by level, which helps track the bottom-most nodes at each horizontal distance.
If two nodes have the same horizontal distance, which one appears in the bottom view?
AThe node at the lower level
BThe node at the higher level
CThe leftmost node
DThe rightmost node
✗ Incorrect
The node at the lower level (deeper in the tree) is visible in the bottom view.
What data structure is used to store the nodes visible in the bottom view?
AStack
BLinked list
CQueue
DMap or dictionary
✗ Incorrect
A map or dictionary stores the latest node at each horizontal distance.
What is the overall time complexity to find the bottom view of a binary tree with n nodes?
AO(n log n)
BO(log n)
CO(n)
DO(n^2)
✗ Incorrect
Each node is visited once, so the time complexity is O(n).
Explain how to compute the bottom view of a binary tree step-by-step.
Think about horizontal distances and level order traversal.
You got /4 concepts.
Describe why level order traversal is important for the bottom view of a binary tree.
Consider how nodes at different depths affect visibility.
You got /3 concepts.
Practice
(1/5)
1. What does the bottom view of a binary tree represent?
easy
A. Nodes visible when the tree is viewed from the bottom
B. Nodes visible when the tree is viewed from the top
C. Nodes visible when the tree is viewed from the left side
D. Nodes visible when the tree is viewed from the right side
Solution
Step 1: Understand the bottom view concept
The bottom view shows nodes visible when looking from the bottom of the tree, meaning the lowest nodes at each horizontal distance.
Step 2: Compare with other views
Top view shows nodes visible from above, left and right views show nodes from sides, so bottom view is unique for bottom visibility.
Final Answer:
Nodes visible when the tree is viewed from the bottom -> Option A
Quick Check:
Bottom view = nodes visible from bottom [OK]
Hint: Bottom view shows lowest nodes at each horizontal distance [OK]
Common Mistakes:
Confusing bottom view with top view
Thinking bottom view shows all leaf nodes
Mixing bottom view with side views
2. Which of the following C++ STL containers is most suitable to store nodes by their horizontal distance for bottom view calculation?
easy
A. std::vector<int>
B. std::map
C. std::stack<int>
D. std::queue<int>
Solution
Step 1: Identify data structure needs
We need to map horizontal distances (int) to node values (int) and keep them sorted by horizontal distance.
Step 2: Choose container
std::map keeps keys sorted and allows key-value pairs, perfect for horizontal distance mapping.
Final Answer:
std::map -> Option B
Quick Check:
Use map for sorted key-value pairs [OK]
Hint: Use map to keep horizontal distances sorted [OK]
Common Mistakes:
Using vector which is index-based, not key-based
Using stack or queue which don't map keys to values
Not sorting horizontal distances
3. What is the bottom view output of the following binary tree?
20
/ \
8 22
/ \ \
5 3 25
/ \
10 14
medium
A. [5, 8, 10, 14, 25]
B. [5, 10, 14, 22, 25]
C. [5, 10, 3, 14, 25]
D. [8, 10, 3, 14, 22]
Solution
Step 1: Assign horizontal distances (HD) to nodes
Root 20 at HD=0; 8 at HD=-1; 22 at HD=1; 5 at HD=-2; 3 at HD=0; 25 at HD=2; 10 at HD=-1; 14 at HD=1.
Step 2: Determine bottom view nodes by HD
At HD=-2: 5; HD=-1: 10 (lower than 8); HD=0: 3 (lower than 20); HD=1: 14 (lower than 22); HD=2: 25.
Final Answer:
[5, 10, 3, 14, 25] -> Option C
Quick Check:
Bottom view nodes by HD = [5,10,3,14,25] [OK]
Hint: Track horizontal distances and pick lowest nodes [OK]
Common Mistakes:
Choosing top nodes instead of bottom nodes
Mixing horizontal distances incorrectly
Ignoring nodes at same horizontal distance but lower level
4. Consider this code snippet for bottom view calculation. What is the error?
void bottomView(Node* root) {
if (!root) return;
std::map hdNodeMap;
std::queue> q;
q.push({root, 0});
while (!q.empty()) {
auto p = q.front(); q.pop();
Node* node = p.first;
int hd = p.second;
if (hdNodeMap.find(hd) == hdNodeMap.end()) {
hdNodeMap[hd] = node->data;
}
if (node->left) q.push({node->left, hd - 1});
if (node->right) q.push({node->right, hd + 1});
}
for (auto it : hdNodeMap) {
std::cout << it.second << " ";
}
}
medium
A. It stores only the first node at each horizontal distance, not the bottom-most
B. It uses queue incorrectly for BFS traversal
C. It does not check for null root
D. It prints nodes in reverse horizontal order
Solution
Step 1: Analyze node storage logic
The code stores node data only if horizontal distance is not already in map, so it keeps first node encountered at that HD.
Step 2: Understand bottom view requirement
Bottom view requires the lowest node at each HD, so we must update map with nodes at lower levels, not just first.
Final Answer:
It stores only the first node at each horizontal distance, not the bottom-most -> Option A
Quick Check:
Bottom view needs last node at HD, not first [OK]
Hint: Update map with latest node at each horizontal distance [OK]
Common Mistakes:
Not updating map for lower level nodes
Assuming BFS order guarantees bottom view
Ignoring horizontal distance updates
5. Given a binary tree, how would you modify the bottom view algorithm to handle nodes with the same horizontal distance and depth, ensuring the rightmost node is chosen in the bottom view?
hard
A. Use a queue for BFS and update map only if node depth is greater, replacing existing node
B. Use DFS and store only the first node encountered at each horizontal distance
C. Use a stack and update map only if node depth is less than current stored depth
D. Use BFS and update map only if node depth is greater or equal, replacing existing node
Solution
Step 1: Understand the problem with same HD and depth
When nodes share horizontal distance and depth, bottom view should show the rightmost node.
Step 2: Modify BFS approach
Use BFS to traverse level by level, and update map if current node's depth is greater or equal to stored node's depth, ensuring rightmost node replaces leftmost at same depth.
Step 3: Implement depth tracking
Track depth along with horizontal distance, update map accordingly during BFS.
Final Answer:
Use BFS and update map only if node depth is greater or equal, replacing existing node -> Option D
Quick Check:
Update map on equal or greater depth for rightmost node [OK]
Hint: Update map if depth >= current to keep rightmost node [OK]
Common Mistakes:
Ignoring depth when updating map
Using DFS which doesn't guarantee level order
Not handling equal depth nodes properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.