📖 Scenario: Imagine you are working on a program that helps visualize a binary tree from different angles. One useful view is the bottom view, which shows the nodes visible when looking at the tree from below.Each node in the tree has a horizontal distance from the root. The bottom view shows the last node at each horizontal distance when viewed from the bottom.
🎯 Goal: You will build a TypeScript program that finds the bottom view of a given binary tree. The program will print the node values visible from the bottom, ordered from left to right.
📋 What You'll Learn
Create a binary tree with the exact structure given.
Use a variable to track horizontal distances.
Implement the logic to find the bottom view using a queue and a map.
Print the bottom view nodes in order from leftmost to rightmost horizontal distance.
💡 Why This Matters
🌍 Real World
Bottom view of a binary tree is useful in graphical representations, network routing visualizations, and understanding hierarchical data from different perspectives.
💼 Career
Understanding tree traversals and views is important for software engineers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the Binary Tree Structure
Create a class called Node with data (number), left (Node or null), and right (Node or null). Then create the binary tree with this exact structure: 20 / \ 8 22 / \ \ 5 3 25 / \ 10 14
DSA Typescript
Hint
Define the Node class with data, left, and right. Then create the nodes exactly as shown to build the tree.
2
Set Up a Map to Track Bottom View Nodes
Create a Map called hdNodeMap to store the latest node data for each horizontal distance (number). Also create a queue array to hold pairs of Node and its horizontal distance (number). Initialize the queue with the root node and horizontal distance 0.
DSA Typescript
Hint
Use a Map to store horizontal distance keys and node data values. Use a queue array to hold objects with node and hd properties.
3
Implement Bottom View Logic Using Level Order Traversal
Use a while loop to process nodes in queue. For each node, update hdNodeMap with the node's data for its horizontal distance. Add the left child to the queue with horizontal distance minus 1, and the right child with horizontal distance plus 1, if they exist.
DSA Typescript
Hint
Use a while loop to remove nodes from the queue. Update the map with the current node's data for its horizontal distance. Add children with updated horizontal distances.
4
Print the Bottom View Nodes from Left to Right
Extract the keys from hdNodeMap, sort them in ascending order, then print the corresponding node data values separated by spaces on one line.
DSA Typescript
Hint
Sort the horizontal distances and print the corresponding node data values separated by spaces.
Practice
(1/5)
1. What does the bottom view of a binary tree represent?
easy
A. Nodes visible when the tree is viewed from the left side
B. Nodes visible when the tree is viewed from the top
C. Nodes visible when the tree is viewed from the bottom
D. Nodes visible when the tree is viewed from the right side
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 C
Quick Check:
Bottom view = visible nodes from below [OK]
Hint: Bottom view shows lowest nodes at each horizontal distance [OK]
Common Mistakes:
Confusing bottom view with top view
Thinking bottom view shows all leaf nodes
Mixing bottom view with left or right side views
2. Which TypeScript data structure is best suited to store nodes by their horizontal distance when computing the bottom view?
easy
A. Array
B. Map (key-value pairs)
C. Set
D. Stack
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 B
Quick Check:
Use Map for horizontal distance to node mapping [OK]
Hint: Use Map to link horizontal distance with nodes [OK]
Common Mistakes:
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
3. What is the output bottom view of the binary tree represented by the following code?
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))
);
medium
A. [5, 8, 3, 10, 25]
B. [5, 10, 3, 22, 25]
C. [8, 3, 10, 22, 25]
D. [5, 3, 10, 22, 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
Hint: Track last node at each horizontal distance [OK]
Common Mistakes:
Choosing first node at horizontal distance instead of last
Mixing horizontal distances incorrectly
Ignoring right child nodes at same horizontal distance
4. Identify the bug in this TypeScript code snippet for bottom view of a binary tree:
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());
}
medium
A. It only stores the first node at each horizontal distance, not the bottom-most
B. It uses shift() which is inefficient for queue
C. It does not check if root is null
D. It returns keys instead of values from the map
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 A
Quick Check:
Bottom view needs last node per hd, code stores first [OK]
Hint: Update map value for every node at horizontal distance [OK]
Common Mistakes:
Not updating map for later nodes at same horizontal distance
Confusing top view logic with bottom view
Ignoring null root check (though present here)
5. Given a binary tree where multiple nodes share the same horizontal distance and depth, how should the bottom view algorithm decide which node to display?
hard
A. Display the node with the smallest value at that horizontal distance
B. Display the node with the largest depth but smallest horizontal distance
C. Display the node that appears first in level order traversal at that horizontal distance
D. Display the node that appears last in level order traversal at that horizontal distance
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 D
Quick Check:
Bottom view = last node per hd in level order [OK]
Hint: Choose last node at horizontal distance in level order [OK]
Common Mistakes:
Choosing node by value instead of position
Selecting first node instead of last at horizontal distance
Confusing horizontal distance with depth
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.