Bottom View of Binary Tree in DSA Java - 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 steps grow when the tree has more nodes?
Analyze the time complexity of the following code snippet.
public void bottomView(Node root) {
if (root == null) return;
Map map = new TreeMap<>();
Queue> queue = new LinkedList<>();
queue.add(new Pair<>(root, 0));
while (!queue.isEmpty()) {
Pair p = queue.poll();
Node node = p.getKey();
int hd = p.getValue();
map.put(hd, node.data);
if (node.left != null) queue.add(new Pair<>(node.left, hd - 1));
if (node.right != null) queue.add(new Pair<>(node.right, hd + 1));
}
for (int val : map.values()) System.out.print(val + " ");
}
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 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 log n)
This means the time grows a bit more than linearly because of sorting keys in the map, but mostly it depends on the number of nodes.
[X] Wrong: "The code only takes O(n) time because it visits each node once."
[OK] Correct: The map used to store horizontal distances is a TreeMap, which sorts keys and takes O(log n) time per insertion, so total time is O(n log n).
Understanding how traversal and data structure choices affect time helps you explain your solutions clearly and choose the best approach in interviews.
"What if we used a HashMap instead of a TreeMap for storing horizontal distances? 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 directions, 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 CQuick Check:
Bottom view = visible nodes from below [OK]
- Confusing bottom view with top view
- Thinking it shows all leaf nodes
- Mixing bottom view with side views
Solution
Step 1: Identify the need to map horizontal distances
We need to store nodes with their horizontal distance to update the bottom-most node at each distance.Step 2: Choose appropriate data structure
A HashMap stores key-value pairs efficiently, mapping horizontal distance (key) to node data (value).Final Answer:
HashMap -> Option AQuick Check:
Use HashMap for horizontal distance mapping [OK]
- Using Stack which is LIFO, not suitable here
- Using Queue alone without mapping distances
- Using ArrayList which doesn't map keys
20
/ \
8 22
/ \ \
5 3 25
/ \
10 14
Solution
Step 1: Assign horizontal distances and track bottom nodes
Horizontal distances: 20(0), 8(-1), 22(1), 5(-2), 3(0), 25(2), 10(-1), 14(1). Bottom nodes at each distance are the lowest nodes: -2:5, -1:10, 0:3, 1:14, 2:25.Step 2: Collect bottom view nodes in order from left to right
Order by horizontal distance: 5, 10, 3, 14, 25.Final Answer:
[5, 10, 3, 14, 25] -> Option BQuick Check:
Bottom view nodes = [5, 10, 3, 14, 25] [OK]
- Not updating bottom node when lower node appears
- Mixing horizontal distances order
- Including nodes not visible from bottom
class Node {
int data;
Node left, right;
Node(int d) { data = d; }
}
void bottomView(Node root) {
Map map = new HashMap<>();
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
Node temp = queue.poll();
int hd = 0; // horizontal distance
map.put(hd, temp.data);
if (temp.left != null) queue.add(temp.left);
if (temp.right != null) queue.add(temp.right);
}
System.out.println(map.values());
}
Solution
Step 1: Analyze horizontal distance usage
The variable 'hd' is reset to 0 inside the loop, so all nodes get the same horizontal distance, which is incorrect.Step 2: Understand horizontal distance tracking
Horizontal distance must be tracked per node and updated when adding children (left: hd-1, right: hd+1).Final Answer:
Horizontal distance 'hd' is reset to 0 inside the loop -> Option DQuick Check:
hd must update per node, not reset [OK]
- Resetting horizontal distance inside loop
- Using Stack instead of Queue for BFS
- Not updating horizontal distance for children
Solution
Step 1: Understand bottom view node selection
Bottom view shows the lowest node at each horizontal distance. When nodes share horizontal distance and depth, the one encountered last in level order (BFS) is the bottom-most.Step 2: Confirm BFS traversal order
Level order traversal visits nodes top to bottom, left to right. Updating map with each node at horizontal distance ensures last node overwrites previous.Final Answer:
Show the node that appears last in level order traversal at that horizontal distance -> Option AQuick Check:
Bottom view = last node at horizontal distance in BFS [OK]
- Choosing first node instead of last at horizontal distance
- Selecting node by value instead of position
- Trying to show multiple nodes per horizontal distance
Ready to prove it?
Pass the test and this pattern moves unknown
