Bottom View of Binary Tree in DSA Typescript - 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: TreeNode | null): number[] {
if (!root) return [];
const queue: Array<[TreeNode, number]> = [[root, 0]];
const map = new Map<number, number>();
while (queue.length) {
const [node, hd] = queue.shift()!;
map.set(hd, node.val);
if (node.left) queue.push([node.left, hd - 1]);
if (node.right) queue.push([node.right, hd + 1]);
}
return Array.from(map.entries())
.sort((a, b) => a[0] - b[0])
.map(entry => entry[1]);
}
This code finds the bottom view of a binary tree by level order traversal, tracking horizontal distances.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop processes each node once.
- How many times: Exactly once per node, so n times for n nodes.
- Inside the loop, map updates and queue operations happen once per node.
- Sorting the map entries happens once after traversal, with entries up to the number of unique horizontal distances.
As the tree grows, the number of nodes n increases, so the loop runs n times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 loop passes + sorting ~10 entries |
| 100 | About 100 loop passes + sorting ~100 entries |
| 1000 | About 1000 loop passes + sorting ~1000 entries |
Pattern observation: The operations grow roughly in direct proportion to n, with sorting adding a small extra cost.
Time Complexity: O(n log n)
This means the time grows a bit faster than the number of nodes because of sorting the horizontal distances after traversal.
[X] Wrong: "The time complexity is just O(n) because we visit each node once."
[OK] Correct: While visiting nodes is O(n), sorting the horizontal distances at the end takes extra time, making the total O(n log n).
Understanding this time complexity helps you explain how traversal and sorting combine in tree problems, a common skill interviewers look for.
"What if we used a balanced tree or priority queue to keep horizontal distances sorted during traversal? 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 of a binary tree includes nodes that are 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 visible from above, left view from left side, right view from right side. Bottom view is distinct as it shows the lowest nodes at each horizontal position.Final Answer:
Nodes visible when the tree is viewed from the bottom -> Option CQuick Check:
Bottom view = visible nodes from below [OK]
- Confusing bottom view with top view
- Thinking bottom view shows all leaf nodes
- Mixing bottom view with left or right side views
Solution
Step 1: Identify the need to map horizontal distances
We need to associate each horizontal distance with the node visible at that distance in the bottom view.Step 2: Choose a data structure for key-value mapping
A Map allows storing keys (horizontal distances) with corresponding node values, enabling easy updates and retrieval.Final Answer:
Map (key-value pairs) -> Option BQuick Check:
Use Map for horizontal distance to node mapping [OK]
- Using Array which is index-based, not key-based
- Using Set which stores unique values but no keys
- Using Stack which is LIFO, not suitable for mapping
class Node {
constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {}
}
const root = new Node(20,
new Node(8,
new Node(5),
new Node(3, null, new Node(10))
),
new Node(22, null, new Node(25))
);
Solution
Step 1: Assign horizontal distances and track nodes
Horizontal distances: 20(0), 8(-1), 22(1), 5(-2), 3(0), 10(1), 25(2). Level order traversal order: 20(0), 8(-1), 22(1), 5(-2), 3(0), 25(2), 10(1). For bottom view, the last node encountered at each hd.Step 2: Determine bottom view nodes by horizontal distance
hd -2:5; -1:8; 0:3 (overwrites 20); 1:10 (overwrites 22); 2:25. Sorted left-to-right: [5, 8, 3, 10, 25].Final Answer:
[5, 8, 3, 10, 25] -> Option AQuick Check:
Bottom view nodes = [5, 8, 3, 10, 25] [OK]
- Choosing first node at horizontal distance instead of last
- Mixing horizontal distances incorrectly
- Ignoring right child nodes at same horizontal distance
function bottomView(root: Node | null): number[] {
if (!root) return [];
const map = new Map();
const queue: Array<{node: Node; hd: number}> = [{node: root, hd: 0}];
while (queue.length) {
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 is not present, so it keeps the first node at that distance.Step 2: Understand bottom view requirement
Bottom view requires the last (bottom-most) node at each horizontal distance, so map should update value every time a node at that hd is found.Final Answer:
It only stores the first node at each horizontal distance, not the bottom-most -> Option AQuick Check:
Bottom view needs last node per hd, code stores first [OK]
- Not updating map for later nodes at same horizontal distance
- Confusing top view logic with bottom view
- Ignoring null root check (though present here)
Solution
Step 1: Understand node selection criteria for bottom view
Bottom view shows the lowest node at each horizontal distance. When multiple nodes share horizontal distance and depth, the one encountered last in level order traversal is visible.Step 2: Clarify why last in level order matters
Level order visits nodes top to bottom, left to right. The last node at a horizontal distance overwrites previous ones, ensuring bottom-most visibility.Final Answer:
Display the node that appears last in level order traversal at that horizontal distance -> Option DQuick Check:
Bottom view = last node per hd in level order [OK]
- Choosing node by value instead of position
- Selecting first node instead of last at horizontal distance
- Confusing horizontal distance with depth
Ready to prove it?
Pass the test and this pattern moves unknown
