Bottom View of Binary Tree in DSA C# - Time & Space Complexity
We want to understand how the time needed grows when we find the bottom view of a binary tree.
How does the number of steps change as the tree gets bigger?
Analyze the time complexity of the following code snippet.
public IList<int> BottomView(TreeNode root) {
var result = new List<int>();
if (root == null) return result;
var queue = new Queue<(TreeNode node, int hd)>();
var map = new SortedDictionary<int, int>();
queue.Enqueue((root, 0));
while (queue.Count > 0) {
var (node, hd) = queue.Dequeue();
map[hd] = node.val; // overwrite to keep bottom-most
if (node.left != null) queue.Enqueue((node.left, hd - 1));
if (node.right != null) queue.Enqueue((node.right, hd + 1));
}
foreach (var val in map.Values) result.Add(val);
return result;
}
This code finds the bottom view of a binary tree by level order traversal and tracking horizontal distances.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node, so n times for n nodes.
- Additional operations: Inserting and updating entries in the map for each node.
- Final loop: Iterates over map entries, at most n entries.
As the tree grows, the number of nodes increases, so the steps grow roughly the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits and map updates |
| 100 | About 100 visits and map updates |
| 1000 | About 1000 visits and map updates |
Pattern observation: The work grows linearly with the number of nodes.
Time Complexity: O(n log n)
This means the time grows a bit more than linearly because each map update takes extra time to keep keys sorted.
[X] Wrong: "The time is just O(n) because we visit each node once."
[OK] Correct: Updating the sorted map for each node takes extra time, so the total is more than just visiting nodes.
Understanding how traversal and data structure choices affect time helps you explain your approach clearly and confidently.
"What if we used a hash map instead of a sorted map? 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 visible when looking at the tree from below, meaning the lowest nodes at each horizontal distance.Step 2: Compare with other views
Top view shows nodes from above, left and right views show nodes from sides, so bottom view is unique for bottom perspective.Final Answer:
The nodes visible when the tree is viewed from the bottom -> Option DQuick Check:
Bottom view = nodes visible from below [OK]
- Confusing bottom view with top view
- Thinking bottom view shows all leaf nodes
- Mixing bottom view with side views
Solution
Step 1: Identify the need to map horizontal distance to node value
We need a structure to store nodes keyed by their horizontal distance for quick updates and lookups.Step 2: Choose the appropriate data structure
A Dictionary allows mapping horizontal distance (int) to node value (int) efficiently.Final Answer:
Dictionary<int, int> -> Option AQuick Check:
Use Dictionary for horizontal distance mapping [OK]
- Using List which doesn't map keys to values
- Using Stack or Queue which are not key-value stores
- Confusing data structure roles
20
/ \
8 22
/ \ \
5 3 25
/ \
10 14
What is the bottom view of this tree?Solution
Step 1: Assign horizontal distances and track bottom nodes
Assign horizontal distances (HD) starting from root (20) at HD=0: - 20(0), 8(-1), 22(1), 5(-2), 3(0), 25(2), 10(-1), 14(1) For each HD, the bottom-most node is: HD -2: 5 HD -1: 10 (overwrites 8) HD 0: 3 (overwrites 20) HD 1: 14 (overwrites 22) HD 2: 25Step 2: Collect bottom view nodes in order of HD
Order HD from left to right: -2, -1, 0, 1, 2 Bottom view nodes: [5, 10, 3, 14, 25]Final Answer:
[5, 10, 3, 14, 25] -> Option CQuick Check:
Bottom view nodes by HD = [5,10,3,14,25] [OK]
- Not updating nodes at same horizontal distance
- Confusing top view with bottom view
- Ignoring horizontal distance ordering
void BottomView(Node root) {
if (root == null) return;
var map = new Dictionary();
var queue = new Queue<(Node node, int hd)>();
queue.Enqueue((root, 0));
while (queue.Count > 0) {
var (node, hd) = queue.Dequeue();
if (!map.ContainsKey(hd)) {
map[hd] = node.data;
}
if (node.left != null) queue.Enqueue((node.left, hd - 1));
if (node.right != null) queue.Enqueue((node.right, hd + 1));
}
foreach (var key in map.Keys.OrderBy(k => k)) {
Console.Write(map[key] + " ");
}
}
Solution
Step 1: Analyze how nodes are stored in the map
The code adds node data to map only if the horizontal distance key is not present, so it keeps the first node encountered at that HD.Step 2: Understand bottom view requires last node at each HD
Bottom view needs the lowest node at each HD, so the map should update the value every time a node at the same HD is found, not only the first.Final Answer:
It only stores the first node at each horizontal distance, not the bottom-most -> Option BQuick Check:
Bottom view needs last node per HD, not first [OK]
- Using if (!map.ContainsKey) instead of always updating
- Ignoring horizontal distance updates
- Not ordering keys before printing (though code does)
Solution
Step 1: Recognize the need to track depth with node values
To ensure the bottom view shows the node with greatest depth at each horizontal distance, we must store depth along with node value.Step 2: Update map only if new node's depth is greater
When visiting a node, compare its depth with stored depth for that horizontal distance; update only if current node is deeper.Final Answer:
Store pairs of (node value, depth) in the map and update only if new node's depth is greater -> Option AQuick Check:
Track depth and update map for deeper nodes [OK]
- Ignoring depth and overwriting incorrectly
- Comparing node values instead of depths
- Changing traversal order without depth tracking
Ready to prove it?
Pass the test and this pattern moves unknown
