Bottom View of Binary Tree in DSA Javascript - 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.
How does the number of nodes affect the work done to get the bottom view?
Analyze the time complexity of the following code snippet.
function bottomView(root) {
if (!root) return [];
const queue = [{ node: root, hd: 0 }];
const map = new Map();
while (queue.length) {
const { node, hd } = queue.shift();
map.set(hd, node.val);
if (node.left) queue.push({ node: node.left, hd: hd - 1 });
if (node.right) queue.push({ node: node.right, hd: hd + 1 });
}
return [...map.values()];
}
This code finds the bottom view of a binary tree by traversing nodes level by level and tracking horizontal distances.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that processes each node once.
- How many times: Exactly once per node in the tree (n times).
As the number of nodes grows, the code visits each node once, so the work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits and updates |
| 100 | About 100 visits and updates |
| 1000 | About 1000 visits and updates |
Pattern observation: The operations increase linearly as the tree size increases.
Time Complexity: O(n)
This means the time to find the bottom view grows in direct proportion to the number of nodes in the tree.
[X] Wrong: "The time complexity is more than linear because we update the map multiple times for the same horizontal distance."
[OK] Correct: Each node is processed once, and map updates happen once per node, so total work is still proportional to the number of nodes.
Understanding this linear time complexity helps you explain how tree traversals work efficiently and shows you can analyze algorithms that use queues and maps together.
"What if we used a recursive depth-first traversal instead of a queue? 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:
Nodes visible when the tree is viewed from the bottom -> Option CQuick 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 suitable data structure for key-value pairs
Map is ideal for storing horizontal distance as key and node data as value with easy insertion and retrieval.Step 2: Check other options
Object ({}) can be used but Map is preferred for ordered keys and better methods; Array and Set are not suitable for key-value pairs.Final Answer:
const map = new Map(); -> Option AQuick Check:
Use Map for horizontal distance mapping [OK]
- Using Array instead of Map for key-value pairs
- Using Set which stores only unique values, not key-value
- Using plain object without considering key types
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
const root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(5);
root.left.right = new Node(3);
root.right.right = new Node(25);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);
// Bottom view logic omitted for brevity
// Assume correct implementation
// Output bottom view nodes in order of horizontal distance
// What is the bottom view output?
Solution
Step 1: Assign horizontal distances and track bottom nodes
Assign horizontal distances: 20(0), 8(-1), 22(1), 5(-2), 3(0), 25(2), 10(-1), 14(1). Bottom view keeps last node at each horizontal distance.Step 2: Determine bottom nodes at each horizontal distance
At -2: 5; at -1: 10 (overwrites 8); at 0: 3 (overwrites 20); at 1: 14 (overwrites 22); at 2: 25.Final Answer:
[5, 10, 3, 14, 25] -> Option BQuick Check:
Bottom view nodes = [5,10,3,14,25] [OK]
- Using first node instead of last at horizontal distance
- Mixing horizontal distances incorrectly
- Ignoring overwriting nodes at same horizontal distance
function bottomView(root) {
const map = new Map();
const queue = [];
queue.push({ node: root, hd: 0 });
while (queue.length > 0) {
const { node, hd } = queue.shift();
if (!map.has(hd)) {
map.set(hd, node.data);
}
if (node.left) queue.push({ node: node.left, hd: hd - 1 });
if (node.right) queue.push({ node: node.right, hd: hd + 1 });
}
return Array.from(map.values());
}Solution
Step 1: Analyze map update condition
The code sets map value only if horizontal distance key is not present, so it keeps first node encountered at that hd.Step 2: Understand bottom view requirement
Bottom view requires last node at each horizontal distance (lowest level), so map should update value every time a node at same hd is found.Final Answer:
It stores only the first node at each horizontal distance, not the bottom-most -> Option AQuick Check:
Bottom view needs last node per hd, code keeps first [OK]
- Not updating map on repeated horizontal distances
- Confusing top view logic with bottom view
- Ignoring queue inefficiency but that doesn't affect correctness
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 one encountered last in level order traversal.Step 2: Reason about traversal order
Level order traversal processes nodes left to right at each level, so last encountered node at hd and level overwrites previous.Final Answer:
Display the node encountered last in level order traversal at that horizontal distance -> Option DQuick Check:
Bottom view uses last node in level order per hd [OK]
- Choosing leftmost or rightmost arbitrarily
- Choosing node by value instead of position
- Ignoring traversal order impact
Ready to prove it?
Pass the test and this pattern moves unknown
