Challenge - 5 Problems
Bottom View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bottom View for a Simple Binary Tree
What is the output of the bottom view traversal for the given binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def bottom_view(root): if not root: return [] from collections import deque queue = deque([(root, 0)]) hd_node_map = {} while queue: node, hd = queue.popleft() hd_node_map[hd] = node.val if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) return [hd_node_map[hd] for hd in sorted(hd_node_map)] root = Node(10) root.left = Node(20) root.right = Node(30) root.left.left = Node(40) root.left.right = Node(50) root.right.right = Node(60) print(bottom_view(root))
Attempts:
2 left
💡 Hint
Remember bottom view shows the last node at each horizontal distance from left to right.
✗ Incorrect
The bottom view keeps the last node encountered at each horizontal distance. For hd -2: 40, hd -1: 20, hd 0: 50 (overwrites 10), hd 1: 30, hd 2: 60.
🧠 Conceptual
intermediateHorizontal Distance in Bottom View
In the bottom view of a binary tree, what does the horizontal distance (hd) represent?
Attempts:
2 left
💡 Hint
Think about how nodes are positioned left or right relative to the root.
✗ Incorrect
Horizontal distance is calculated by moving left (hd - 1) or right (hd + 1) from the root, representing horizontal position.
🔧 Debug
advancedIdentify the Error in Bottom View Implementation
What error will this code produce when computing the bottom view of a binary tree?
DSA Python
def bottom_view(root): if not root: return [] queue = [(root, 0)] hd_node_map = {} while queue: node, hd = queue.pop() hd_node_map[hd] = node.val if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) return [hd_node_map[hd] for hd in sorted(hd_node_map)]
Attempts:
2 left
💡 Hint
Check how nodes are removed from the queue and how that affects traversal order.
✗ Incorrect
Using pop() removes from the end (LIFO), making traversal depth-first and right-to-left, so the last node at each hd is not the bottom-most but the last visited in reverse order.
❓ Predict Output
advancedBottom View Output for Complex Tree
What is the output of the bottom view traversal for this binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def bottom_view(root): from collections import deque queue = deque([(root, 0)]) hd_node_map = {} while queue: node, hd = queue.popleft() hd_node_map[hd] = node.val if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) return [hd_node_map[hd] for hd in sorted(hd_node_map)] root = Node(1) root.left = Node(2) root.right = Node(3) root.left.right = Node(4) root.left.right.right = Node(5) root.left.right.right.right = Node(6) print(bottom_view(root))
Attempts:
2 left
💡 Hint
Track horizontal distances carefully as nodes go deeper on the right side of left subtree.
✗ Incorrect
Horizontal distances: 1 at hd 0, 2 at hd -1, 3 at hd 1, 4 at hd 0 (overwrites 1), 5 at hd 1 (overwrites 3), 6 at hd 2. Final bottom view is [2, 4, 5, 6].
🚀 Application
expertNumber of Nodes in Bottom View
Given a binary tree, how many nodes will be in the bottom view if the tree is a perfect binary tree of height 3?
Attempts:
2 left
💡 Hint
Consider the horizontal distances for a perfect binary tree of height 3.
✗ Incorrect
A perfect binary tree of height 3 has 15 nodes. Horizontal distances range from -3 to 3, so bottom view has 7 nodes. Nodes at hd -3, -2, -1, 0, 1, 2, 3 are visible. The bottom view includes one node per horizontal distance, so total is 7.
