ProgramGuru
| Input Tree | Bottom View Output | Description |
|---|---|---|
| [20, 8, 22, 5, 3, null, 25, null, null, 10, 14]
|
[5, 10, 3, 14, 25] | Standard binary tree showing nodes visible from the bottom across horizontal distances |
| [1]
|
[1] | Single node tree; only the root is visible from the bottom |
| [] | [] | Empty tree; no nodes to display in bottom view |
| [1, 2, null, 3, null, 4]
|
[4, 2, 1] | Left-skewed tree; last node at each horizontal distance is shown |
| [1, null, 2, null, null, null, 3]
|
[1, 2, 3] | Right-skewed tree; all nodes fall on unique horizontal distances |
| [1, 2, 3, 4, 5, 6, 7]
|
[4, 2, 6, 3, 7] | Complete binary tree; bottom-most nodes overwrite top ones at each horizontal distance |
We are given a binary tree, and our task is to find its bottom view. Imagine standing below the binary tree and looking up — the nodes that are visible from that viewpoint form the bottom view. In other words, for each vertical line (or horizontal distance from the root), we want the bottommost node visible in that column.
This means we need to process nodes based on their horizontal distances and pick the last node (deepest node) at each distance level as seen in a level-order (breadth-first) traversal.
To process nodes level by level, we use a queue. Each element in the queue will be a pair of the node and its horizontal distance (HD) from the root.
We maintain a map or dictionary that maps each horizontal distance to the latest node value we see at that distance. Since we are doing level-order traversal, nodes at lower levels naturally overwrite those at higher levels for the same horizontal distance.
For every node, we:
Finally, we collect the node values from the map, sorted by the horizontal distance (from leftmost to rightmost), to form our bottom view.
Let’s consider this binary tree:
20
/ 8 22
/ 5 3 25
/ 10 14
Horizontal distances (HD) from root (20 at HD = 0):
During traversal, we keep updating values at each HD. At the end, the bottom view map (sorted by HD) is:
Bottom view: [5, 8, 10, 14, 25]
If the tree is empty (root is null), we simply return an empty list as there are no nodes to show.
If there is only one node, the bottom view will contain just that node.
Every node has a unique horizontal distance, so all will be visible in the bottom view. For example:
1
/
2
/
3
Bottom view: [3, 2, 1]
Same as left-skewed but horizontal distances increase:
1
2
3
Bottom view: [1, 2, 3]
When multiple nodes have the same horizontal distance, only the bottommost (deepest and latest in level order) is retained.
The key to solving the bottom view problem is combining level-order traversal with horizontal distance tracking. This ensures we always get the bottommost node for each vertical column. By thinking in terms of vertical slices and processing depth, we make the logic intuitive and beginner-friendly.
Always test with skewed trees and overlapping nodes to make sure your solution handles all edge cases.
queue and enqueue the root node along with its horizontal distance (hd = 0).hdMap) to store the latest node value at each horizontal distance.hdMap[hd] with the node's value (this ensures the bottom-most node at that hd is recorded).hd - 1; if it has a right child, enqueue it with hd + 1.hdMap and record their corresponding values. The ordered values form the bottom view of the binary tree.#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello from C!\n");
return 0;
}
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗