DSA Tracker

Problem

Medium

Bottom View of Binary Tree

A medium Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
2
Core for
4 roles

The problem

Given the root of a binary tree, return the values of the nodes visible from the bottom when looking at the tree from below. A node is visible from the bottom if it is the deepest node in its vertical column.

Example 1

Input
[1,2,3,4,5,6,7]
Output
[4,5,6,3,7]
Why
The deepest node in each vertical column is visible from below: 4 at -2, 5 at -1, 6 at 0, 3 at 1, 7 at 2.

Example 2

Input
[1,2,3,null,5,null,7]
Output
[2,5,3,7]
Why
Bottom view: 2 at column -1, 5 at column 0 (deeper than 1), 3 at column 1, 7 at column 2.

Example 3

Input
[1]
Output
[1]
Why
The single root node is visible from below.

Constraints

  • The number of nodes is in the range [1, 1000].
  • -1000 <= Node.val <= 1000

How to think about it

Updated 2026-09-29

The bottom view is the exact visual inverse of the top view: it wants the latest, deepest node for each horizontal column. In level order traversal, visiting row by row means later nodes in the same column overwrite earlier ones. The final overwrite left standing in each column is precisely the bottom view.

Approaches, worst first

  1. DFS with depth comparison

    time O(n log n) · space O(n)

    Traverse tree tracking `(col, depth)`. Store `col -> (val, depth)` in a map, updating the entry whenever `depth >= existingDepth`. Output columns in sorted horizontal order.

  2. BFS unconditional overwriteWrite this one

    time O(n) · space O(n)

    Queue tuples of `(node, col)`. In a map of `col -> val`, unconditionally set `map[col] = node.val` during traversal. Maintain `minCol` and `maxCol` to iterate and output the final values from left to right without sorting.

Where people lose marks · 3
  • In DFS, using strict `>` depth check instead of `>=` can fail to pick the rightmost node when two nodes share the same lowest row and column.
  • Assuming bottom view nodes must be leaf nodes; an internal node whose children branch into adjacent columns can still be the bottom-most node of its own column.
  • Sorting map keys instead of using running min/max column bounds increases runtime.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Asked at

AccoliteAmazonCouponDuniaFlipkartOYO RoomsPaytmWalmart

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .