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 0 to the root node. For the left child, horizontal distance is parent's distance minus 1. For the right child, it is parent's distance plus 1.
Click to reveal answer
intermediate
Which data structure is commonly used to track nodes and their horizontal distances during bottom view traversal?
A queue is used for level order traversal, and a map (or dictionary) stores the latest node at each horizontal distance.
Click to reveal answer
intermediate
Why does the bottom view use level order traversal instead of inorder or preorder?
Level order traversal visits nodes level by level, ensuring the bottom-most node at each horizontal distance is recorded last, which is needed for the bottom view.
Step 2: Collect bottom view nodes from left to right
From leftmost (-1) to rightmost (3), nodes are 2, 4, 6, 5, 7.
Final Answer:
[2, 4, 6, 5, 7] -> Option C
Quick Check:
Bottom view nodes left to right = [2, 4, 6, 5, 7] [OK]
Hint: Track horizontal distances and pick last node at each [OK]
Common Mistakes:
Including nodes not visible from bottom
Confusing horizontal distances
Listing nodes in wrong order
4. Identify the error in this C code snippet for bottom view calculation:
void bottomView(Node* root) {
map hdNodeMap;
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) {
cout << it.second << " ";
}
}
medium
A. Output loop should print keys instead of values
B. Queue is not initialized properly
C. Left and right child checks are reversed
D. Using map prevents updating bottom nodes at same horizontal distance
Solution
Step 1: Analyze map usage for horizontal distances
The code inserts node data only if horizontal distance is not present, so it keeps the first node seen, which is top view behavior.
Step 2: Correct approach for bottom view
For bottom view, update the map every time a node at the same horizontal distance is found, so the last node at that distance remains.
Final Answer:
Using map prevents updating bottom nodes at same horizontal distance -> Option D
Quick Check:
Bottom view needs updating map entries [OK]
Hint: Update map for every node at horizontal distance for bottom view [OK]
Common Mistakes:
Using if condition to insert only once (top view logic)
Not updating map with lower nodes
Confusing top and bottom view logic
5. Given a binary tree where multiple nodes share the same horizontal distance and level, how should the bottom view algorithm decide which node to show?
hard
A. Show the node that appears last in level order traversal at that horizontal distance
B. Show the node that appears first in level order traversal at that horizontal distance
C. Show the node with the smallest value at that horizontal distance
D. Show the node with the largest value at that horizontal distance
Solution
Step 1: Understand node conflicts at same horizontal distance and level
When nodes share horizontal distance and level, bottom view shows the one visible from bottom, which is the last node encountered in level order at that position.
Step 2: Use level order traversal order to decide
Level order traversal visits nodes top to bottom, left to right. The last node at a horizontal distance overwrites previous ones in the map.
Final Answer:
Show the node that appears last in level order traversal at that horizontal distance -> Option A
Quick Check:
Bottom view picks last node at horizontal distance [OK]
Hint: Last node at horizontal distance in level order is bottom view node [OK]
Common Mistakes:
Picking first node instead of last
Choosing node by value instead of position
Ignoring traversal order impact
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.