Bottom view
Intermediate · 9 min · code in PHP, JavaScript, Java and Go
Where you will meet this in real life
The bottom view is the opposite of the top view: looking up from underneath, you see the lowest node in each vertical column. It shows how the same column idea can answer "what is on top" or "what is at the bottom" just by changing one rule.
Practical use cases
- Web app: Latest snapshot per column in a timeline. A project-planning page draws a timeline where tasks hang below milestones. For the summary strip, the page shows only the lowest, most recent task in each column. Overwriting the answer whenever a deeper node appears gives the strip with no extra sorting.
- Web app: Street-level view of a shopping mall map. An indoor mall map is stored as a tree of floors and shops. The "street view" tooltip should show the nearest shop in each column, which is the lowest one. Taking the bottom view lets the tooltip list exactly one shop per column.
- System: Visibility in rendering engines. Some rendering and layout engines need the front-most item per column when drawing from below, such as the nearest object in a scene. Choosing the last seen node per column gives that answer in one pass. Only one value is kept per column, so memory use stays small.
The story: The Diwali Lantern Tree
Every Diwali, Mira's mohalla hung paper lanterns on a big frame shaped like a tree: one lantern at the top, two below it, more below those. Mira lay on the terrace floor under the frame and looked straight up. "Kabir, I can't see all of them! Some lanterns hide behind others."
Kabir squinted. "Of course. Look along any straight vertical line from the floor to the roof. You only see the lowest lantern on that line. The ones above it are hidden." Mira wanted to write down exactly what the people on the ground could see, from the far left to the far right.
Kabir gave every lantern a column number: the top lantern is column 0, a lantern down-left is one less, a lantern down-right is one more. Then he walked through the lanterns floor by floor. Each time he met a lantern, he wrote it in its column, erasing whatever was there before, because the newer one is lower. In the end each column held its lowest lantern. That list is called the bottom view of a tree.
How to spot this problem, and where you need it
Several nodes sit in the same vertical column and you need the lowest one in each, the one that would cover the others when seen from below.
Signs you need it
- The wording says "bottom view", "lowest node on each vertical line", or "what is visible from below".
- You already compute column positions for a top view and now need the opposite choice.
- Your drawing shows the older, higher node when the newest, deepest node should hide it.
Where to use it in a project
- Timeline and planning charts that show only the most recent item in each column.
- Layout engines and map tools that must pick the front-most element per position.
The solution
Assign columns as in the top view: root 0, left child minus one, right child plus one. Walk level by level with a queue. Every time you reach a node, overwrite the stored answer for its column. Because deeper levels come later, the last write per column is the lowest node. Read the columns from left to right.
Payoff: Before: every one of 40 nodes checked against the others by hand. After: one pass and one overwrite per node gives exactly 9 visible items.
Explain it like I am 8
Stand under a tree house and look up. Where branches overlap, you only see the lowest branch, because it covers the ones above. Write down what you see from left to right. That is the bottom view.
How it works, step by step
- Give each node a column number: root = 0, left child = column - 1, right child = column + 1.
- Visit the tree level by level (breadth-first, using a queue), carrying the column number along with each node.
- Keep a table
column → value. Every time you visit a node, overwrite its column with this node. Later levels are lower, so they win. - When the queue is empty, read the table from the smallest column to the largest. That is the bottom view.
The code in four languages
PHP
<?php
class Node {
public ?Node $left = null;
public ?Node $right = null;
public function __construct(public int $val) {}
}
function insert(?Node $n, int $v): Node {
if ($n === null) return new Node($v);
if ($v < $n->val) $n->left = insert($n->left, $v);
else $n->right = insert($n->right, $v);
return $n;
}
function bottomView(?Node $root): array {
if ($root === null) return [];
$seen = []; // column => value of the lowest node so far
$queue = [[$root, 0]]; // level order: each item carries its column
while ($queue) {
[$node, $col] = array_shift($queue);
$seen[$col] = $node->val; // overwrite: later = lower
if ($node->left) $queue[] = [$node->left, $col - 1];
if ($node->right) $queue[] = [$node->right, $col + 1];
}
ksort($seen); // left-most column first
return array_values($seen);
}
$root = null;
foreach ([8, 3, 10, 1, 6, 14, 4, 7, 13] as $v) $root = insert($root, $v);
echo implode(' ', bottomView($root)), PHP_EOL; // 1 4 6 13 14JavaScript
class Node {
constructor(val) { this.val = val; this.left = null; this.right = null; }
}
function insert(n, v) {
if (n === null) return new Node(v);
if (v < n.val) n.left = insert(n.left, v);
else n.right = insert(n.right, v);
return n;
}
function bottomView(root) {
if (root === null) return [];
const seen = new Map(); // column -> value of the lowest node so far
const queue = [[root, 0]]; // level order: each item carries its column
while (queue.length) {
const [node, col] = queue.shift();
seen.set(col, node.val); // overwrite: later = lower
if (node.left) queue.push([node.left, col - 1]);
if (node.right) queue.push([node.right, col + 1]);
}
const cols = [...seen.keys()].sort((a, b) => a - b); // left-most column first
return cols.map(c => seen.get(c));
}
let root = null;
for (const v of [8, 3, 10, 1, 6, 14, 4, 7, 13]) root = insert(root, v);
console.log(bottomView(root).join(' ')); // 1 4 6 13 14Java
import java.util.*;
import java.util.stream.*;
public class Main {
static class Node {
int val;
Node left, right;
Node(int val) { this.val = val; }
}
static Node insert(Node n, int v) {
if (n == null) return new Node(v);
if (v < n.val) n.left = insert(n.left, v);
else n.right = insert(n.right, v);
return n;
}
static Node buildDemo() {
Node root = null;
for (int v : new int[]{8, 3, 10, 1, 6, 14, 4, 7, 13}) root = insert(root, v);
return root;
}
static String join(List<Integer> xs) {
return xs.stream().map(String::valueOf).collect(Collectors.joining(" "));
}
record Item(Node node, int col) {}
static List<Integer> bottomView(Node root) {
if (root == null) return new ArrayList<>();
Map<Integer, Integer> seen = new TreeMap<>(); // column -> value of the lowest node so far
Queue<Item> queue = new ArrayDeque<>(); // level order: each item carries its column
queue.add(new Item(root, 0));
while (!queue.isEmpty()) {
Item item = queue.poll();
seen.put(item.col(), item.node().val); // overwrite: later = lower
if (item.node().left != null) queue.add(new Item(item.node().left, item.col() - 1));
if (item.node().right != null) queue.add(new Item(item.node().right, item.col() + 1));
}
return new ArrayList<>(seen.values()); // left-most column first (TreeMap order)
}
public static void main(String[] args) {
System.out.println(join(bottomView(buildDemo()))); // 1 4 6 13 14
}
}Go
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
type Node struct {
val int
left, right *Node
}
func insert(n *Node, v int) *Node {
if n == nil {
return &Node{val: v}
}
if v < n.val {
n.left = insert(n.left, v)
} else {
n.right = insert(n.right, v)
}
return n
}
func buildDemo() *Node {
var root *Node
for _, v := range []int{8, 3, 10, 1, 6, 14, 4, 7, 13} {
root = insert(root, v)
}
return root
}
type item struct {
node *Node
col int
}
func bottomView(root *Node) []int {
if root == nil {
return []int{}
}
seen := map[int]int{} // column -> value of the lowest node so far
queue := []item{{root, 0}} // level order: each item carries its column
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
seen[cur.col] = cur.node.val // overwrite: later = lower
if cur.node.left != nil {
queue = append(queue, item{cur.node.left, cur.col - 1})
}
if cur.node.right != nil {
queue = append(queue, item{cur.node.right, cur.col + 1})
}
}
cols := []int{}
for c := range seen {
cols = append(cols, c)
}
sort.Ints(cols) // left-most column first
out := []int{}
for _, c := range cols {
out = append(out, seen[c])
}
return out
}
// joinInts glues the numbers together with a separator.
func joinInts(xs []int, sep string) string {
parts := make([]string, len(xs))
for i, v := range xs {
parts[i] = strconv.Itoa(v)
}
return strings.Join(parts, sep)
}
func main() {
fmt.Println(joinInts(bottomView(buildDemo()), " ")) // 1 4 6 13 14
}Time: O(n) · Extra space: O(n). Every node is visited once; the queue and the column table hold at most all nodes.
Watch out for
- Using depth-first order and overwriting blindly. A deeper-but-visited-earlier node can be replaced by a shallower one. Use level order (or compare depths).
- Forgetting to sort the columns before printing, so the view comes out in discovery order instead of left to right.
- Giving both children the same column. Left is column - 1, right is column + 1.
Quick check
- In the bottom view, which node of a column is shown?
- The first one met from the root
- The lowest one in that column
- The one with the biggest value
- Why do we traverse level by level?
- So later (lower) nodes overwrite earlier ones
- Because recursion is not allowed
- To sort the values
- A left child of a node in column 2 goes to column...
- 3
- 1
- 2