Bottom View of Binary Tree in DSA Go - 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.
func bottomView(root *Node) []int {
if root == nil {
return []int{}
}
queue := []Pair{{root, 0}}
hdMap := map[int]int{}
for len(queue) > 0 {
p := queue[0]
queue = queue[1:]
hdMap[p.hd] = p.node.data
if p.node.left != nil {
queue = append(queue, Pair{p.node.left, p.hd - 1})
}
if p.node.right != nil {
queue = append(queue, Pair{p.node.right, p.hd + 1})
}
}
// Extract bottom view from hdMap
return extractBottomView(hdMap)
}
This code uses a breadth-first search to visit each node once, tracking horizontal distances to find the bottom view.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that processes each node in the queue once.
- How many times: Exactly once per node in the tree (n times).
As the number of nodes (n) increases, the loop runs once for each node.
| 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 work grows directly with the number of nodes; doubling nodes roughly doubles the work.
Time Complexity: O(n)
This means the time to find the bottom view grows linearly with 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 inside that single pass, so total operations still scale linearly with nodes.
Understanding this linear time complexity helps you explain efficient tree traversals and map usage clearly, a skill valued in many coding challenges.
"What if we used a depth-first search instead of breadth-first search? 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 view shows nodes from above, left view from left side, right view from right side. Bottom view is unique for the bottom perspective.Final Answer:
Nodes visible when the tree is viewed from the bottom -> Option AQuick Check:
Bottom view = Nodes visible from bottom [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 distances
To find bottom view, we track nodes by their horizontal distance from root, so we need a structure to map distance to node value.Step 2: Choose the correct data structure
A map (dictionary) stores key-value pairs, perfect for horizontal distance as key and node value as value.Final Answer:
Map (dictionary) -> Option CQuick Check:
Use map to store horizontal distance -> node [OK]
- Using stack which is LIFO, not suitable here
- Using queue only for traversal, not for mapping
- Using slice without keys for horizontal distance
20
/ \
8 22
/ \ \
5 3 25
/ \
10 14
Solution
Step 1: Assign horizontal distances and track bottom nodes
Assign horizontal distances (HD) starting from root (20) as 0. Left child decreases HD by 1, right child increases by 1. - 20 (HD=0) - 8 (HD=-1), 22 (HD=1) - 5 (HD=-2), 3 (HD=0), 25 (HD=2) - 10 (HD=-1), 14 (HD=1)Step 2: Determine bottom view nodes by last node at each HD
At HD=-2: 5 HD=-1: 10 (overwrites 8) HD=0: 3 (overwrites 20) HD=1: 14 (overwrites 22) HD=2: 25 Bottom view = [5, 10, 3, 14, 25]Final Answer:
[5, 10, 3, 14, 25] -> Option BQuick Check:
Bottom view nodes by HD = [5,10,3,14,25] [OK]
- Taking first node at HD instead of last
- Mixing horizontal distances
- Ignoring nodes on right side
func bottomView(root *Node) []int {
if root == nil {
return nil
}
queue := []*Node{root}
hdMap := make(map[int]int)
hd := 0
for len(queue) > 0 {
node := queue[0]
queue = queue[1:]
hdMap[hd] = node.val
if node.left != nil {
queue = append(queue, node.left)
hd--
}
if node.right != nil {
queue = append(queue, node.right)
hd++
}
}
// code to extract and return bottom view omitted
return nil
}Solution
Step 1: Analyze horizontal distance tracking
The variable hd is a single integer updated globally, but each node can have different hd. It should be tracked per node, e.g., by storing node and hd together in queue.Step 2: Identify impact of error
Because hd is not per node, hdMap updates will be incorrect, leading to wrong bottom view.Final Answer:
Horizontal distance (hd) is not tracked per node, causing wrong mapping -> Option DQuick Check:
Track hd per node, not globally [OK]
- Updating hd globally instead of per node
- Ignoring nil root check
- Returning nil without result
Solution
Step 1: Understand the requirement
To correctly get bottom view for hd [-1,1], must visit entire tree as in-range hd nodes may be reached via out-of-range parents.Step 2: Standard traversal with filtered output
Run standard level order, update map for all hd. When printing, iterate only over keys -1 to 1 in sorted order.Final Answer:
Store all nodes in map, then filter keys between -1 and 1 before printing -> Option AQuick Check:
Traverse all, map all hd, print filtered keys [OK]
- Ignoring out-of-range nodes during traversal misses deeper in-range nodes
- Using stack breaks level order
- Preorder traversal disrupts bottom view order
Ready to prove it?
Pass the test and this pattern moves unknown
