Bottom View of a Binary Tree - Iterative Approach

Visualization Player

Problem Statement

Given a binary tree, return the bottom view of the tree. The bottom view consists of the nodes that are visible when the tree is viewed from the bottom. For each horizontal distance from the root, the node that is at the lowest level (deepest) will be visible. Your task is to determine these visible nodes using an iterative approach.

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

Solution

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
This ensures that for each column (HD), the bottommost node is the one last seen in traversal.

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.

Algorithm Steps

  1. If the binary tree is empty, return an empty result.
  2. Initialize a queue and enqueue the root node along with its horizontal distance (hd = 0).
  3. Initialize an empty map (hdMap) to store the latest node value at each horizontal distance.
  4. While the queue is not empty, dequeue an element (node, hd).
  5. Update hdMap[hd] with the node's value (this ensures the bottom-most node at that hd is recorded).
  6. If the node has a left child, enqueue it with hd - 1; if it has a right child, enqueue it with hd + 1.
  7. After processing all nodes, sort the keys of hdMap and record their corresponding values. The ordered values form the bottom view of the binary tree.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Hello from C!\n");
    return 0;
}

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...