Bottom View of Binary Tree
Bottom View of Binary Tree is a medium trees & bst problem solved with the views pattern.
The best approach, bfs with columns (last seen wins), runs in O(n) time and O(n) space.
Below are 2 approaches in Java, from dfs tracking the deepest node per column up.
Problem
Return the bottom view of a binary tree: for each vertical column, the node you see when looking up from below, ordered from left to right.
Examples
Example 1
- Input
root = [20, 8, 22, 5, 3, null, 25, null, null, 10, 14]- Output
[5, 10, 3, 14, 25]
Example 2
- Input
root = [1, 2, 3]- Output
[2, 1, 3]
Constraints
- The tree has
1to10^5nodes. - If two nodes share the deepest position in a column, take the later one in level order.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.
| Approach | Time | Space |
|---|---|---|
| DFS tracking the deepest node per column | O(n log n) | O(n) |
| BFS with columns (last seen wins) | O(n) | O(n) |
1DFS tracking the deepest node per column
O(n log n)O(n)DFS with (column, depth). For each column keep the node with the greatest depth; on equal depth, keep the later visit.
- Replace when depth >= stored depth.
- Read the TreeMap in order.
class Solution {
public ArrayList<Integer> bottomView(TreeNode root) {
TreeMap<Integer, int[]> best = new TreeMap<>();
dfs(root, 0, 0, best);
ArrayList<Integer> out = new ArrayList<>();
for (int[] v : best.values()) out.add(v[1]);
return out;
}
private void dfs(TreeNode n, int col, int depth, TreeMap<Integer, int[]> best) {
if (n == null) return;
int[] cur = best.get(col);
if (cur == null || depth >= cur[0]) best.put(col, new int[] { depth, n.val });
dfs(n.left, col - 1, depth + 1, best);
dfs(n.right, col + 1, depth + 1, best);
}
}2BFS with columns (last seen wins)
O(n)O(n)In level order, overwrite each column's value every time you reach it. The last write is the deepest node, and later in level order on ties.
- Queue of (node, col); map.put(col, val) always.
- Output from min column to max column.
class Solution {
public ArrayList<Integer> bottomView(TreeNode root) {
Map<Integer, Integer> last = new HashMap<>();
Queue<TreeNode> q = new ArrayDeque<>();
Queue<Integer> cols = new ArrayDeque<>();
q.add(root); cols.add(0);
int min = 0, max = 0;
while (!q.isEmpty()) {
TreeNode n = q.poll();
int c = cols.poll();
last.put(c, n.val);
min = Math.min(min, c); max = Math.max(max, c);
if (n.left != null) { q.add(n.left); cols.add(c - 1); }
if (n.right != null) { q.add(n.right); cols.add(c + 1); }
}
ArrayList<Integer> out = new ArrayList<>();
for (int c = min; c <= max; c++) out.add(last.get(c));
return out;
}
}Edge cases to test
- Nodes at the same column and depth
Hints
Hint 1
It is the top view with 'last seen wins' instead of 'first seen wins'.
FAQ
What is the best time complexity for Bottom View of Binary Tree?
BFS with columns (last seen wins) runs in O(n) time and O(n) extra space.
Which pattern does Bottom View of Binary Tree use?
It is a trees & bst problem that uses the views pattern. Other problems with the same pattern: Top View of Binary Tree, Left View of Binary Tree, Binary Tree Right Side View.
Is there a brute force solution for Bottom View of Binary Tree?
Yes. DFS tracking the deepest node per column takes O(n log n) time and O(n) space. DFS with (column, depth).
Which edge cases should I test for Bottom View of Binary Tree?
Nodes at the same column and depth.