Bottom View of Binary Tree in DSA C - Time & Space Complexity
We want to understand how the time needed to find the bottom view of a binary tree changes as the tree grows.
Specifically, how does the work increase when the number of nodes increases?
Analyze the time complexity of the following code snippet.
// Structure for tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to print bottom view
void bottomView(struct Node* root) {
if (!root) return;
// Use queue for level order traversal
// Use map to store horizontal distance and node data
// Traverse tree and update map
// Print map values
}
This code performs a level order traversal of the tree, tracking horizontal distances to find the bottom view nodes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Level order traversal visiting each node once.
- How many times: Each node is processed exactly one time.
As the number of nodes increases, the traversal visits each node once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The operations increase linearly as the tree size grows.
Time Complexity: O(n)
This means the time to find the bottom view grows directly in proportion to the number of nodes in the tree.
[X] Wrong: "The bottom view requires checking all paths, so it must be more than linear time."
[OK] Correct: The level order traversal visits each node once, and updating the map is constant time per node, so total time stays linear.
Understanding how tree traversals scale helps you explain your approach clearly and confidently in interviews.
"What if we used a recursive depth-first traversal instead of level order? How would the time complexity change?"
Practice
bottom view of a binary tree represent?Solution
Step 1: Understand the bottom view concept
The bottom view shows nodes that are visible when looking at the tree from below, ignoring nodes hidden behind others.Step 2: Compare with other views
Top, left, and right views show nodes from different angles, but bottom view specifically shows the lowest nodes at each horizontal distance.Final Answer:
Nodes visible when the tree is viewed from the bottom -> Option AQuick Check:
Bottom view = nodes visible from below [OK]
- Confusing bottom view with top view
- Thinking it shows all leaf nodes only
- Mixing bottom view with side views
Solution
Step 1: Identify the need for horizontal distance mapping
To find the bottom view, we map each node's horizontal distance to its value, updating with the lowest node at that distance.Step 2: Choose the suitable data structure
A hash map (dictionary) stores horizontal distance as key and node value as value, allowing quick updates and lookups.Final Answer:
Hash Map (Dictionary) -> Option BQuick Check:
Use hash map to map horizontal distance to node [OK]
- Using stack which is LIFO and not suitable here
- Using queue alone without mapping distances
- Using linked list which is inefficient for lookups
1
/ \
2 3
\ \
4 5
\ \
6 7
Options show node values from left to right in bottom view.Solution
Step 1: Assign horizontal distances and track bottom nodes
Assign horizontal distances: 1=0, 2=-1, 3=1, 4=0, 5=2, 6=1, 7=3. Bottom nodes at each distance are: -1:2, 0:4, 1:6, 2:5, 3:7.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 CQuick Check:
Bottom view nodes left to right = [2, 4, 6, 5, 7] [OK]
- Including nodes not visible from bottom
- Confusing horizontal distances
- Listing nodes in wrong order
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 << " ";
}
}
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 DQuick Check:
Bottom view needs updating map entries [OK]
- Using if condition to insert only once (top view logic)
- Not updating map with lower nodes
- Confusing top and bottom view logic
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 AQuick Check:
Bottom view picks last node at horizontal distance [OK]
- Picking first node instead of last
- Choosing node by value instead of position
- Ignoring traversal order impact
Ready to prove it?
Pass the test and this pattern moves unknown
