Bottom View of a Binary Tree
Problem and examples
| 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 |
Explore Bottom View of a Binary Tree - Iterative
Load an example or enter your own input. Use the player controls to follow each change.
Understanding the Problem
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.
Step-by-Step Solution with Example
step 1: Use a Queue for Level Order 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.
step 2: Track Horizontal Distances in a Map
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.
step 3: Traverse the Tree and Fill the Map
For every node, we:
- Dequeue it and update the map with its value for its horizontal distance
- Enqueue its left child with HD - 1
- Enqueue its right child with HD + 1
step 4: Sort the Map by Horizontal Distance
Finally, we collect the node values from the map, sorted by the horizontal distance (from leftmost to rightmost), to form our bottom view.
step 5: Apply the Steps to an Example
Let’s consider this binary tree:
20
/ 8 22
/ 5 3 25
/ 10 14
Horizontal distances (HD) from root (20 at HD = 0):
- 5 → HD = -2
- 8 → HD = -1
- 10 → HD = 0
- 14 → HD = 1
- 25 → HD = 2
During traversal, we keep updating values at each HD. At the end, the bottom view map (sorted by HD) is:
- -2 → 5
- -1 → 8
- 0 → 10
- 1 → 14
- 2 → 25
Bottom view: [5, 8, 10, 14, 25]
Edge Cases
Empty Tree
If the tree is empty (root is null), we simply return an empty list as there are no nodes to show.
Single Node Tree
If there is only one node, the bottom view will contain just that node.
Left-Skewed Tree
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]
Right-Skewed Tree
Same as left-skewed but horizontal distances increase:
1
2
3
Bottom view: [1, 2, 3]
Nodes Overlapping at Same HD
When multiple nodes have the same horizontal distance, only the bottommost (deepest and latest in level order) is retained.
Finally
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.
- If the binary tree is empty, return an empty result.
- Initialize a
queueand enqueue the root node along with its horizontal distance (hd = 0). - Initialize an empty map (
hdMap) to store the latest node value at each horizontal distance. - While the queue is not empty, dequeue an element (node, hd).
- Update
hdMap[hd]with the node's value (this ensures the bottom-most node at that hd is recorded). - If the node has a left child, enqueue it with
hd - 1; if it has a right child, enqueue it withhd + 1. - After processing all nodes, sort the keys of
hdMapand record their corresponding values. The ordered values form the bottom view of the binary tree.
Implementation and efficiency
C implementation
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello from C!\n");
return 0;
}C++ implementation
#include <iostream>
using namespace std;
int main() {
cout << "Hello from C++!" << endl;
return 0;
}Python implementation
def main():
print("Hello from Python!")
if __name__ == "__main__":
main()Java implementation
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello from Java!");
}
}JavaScript implementation
function main() {
console.log("Hello from JavaScript!");
}
main();Go implementation
package main
import "fmt"
func main() {
fmt.Println("Hello from Go!")
}Rust implementation
fn main() {
println!("Hello from Rust!");
}Kotlin implementation
fun main() {
println("Hello from Kotlin!")
}Swift implementation
import Foundation
print("Hello from Swift!")TypeScript implementation
function main(): void {
console.log("Hello from TypeScript!");
}
main();