Bottom view of BT
Pattern: Views and boundaries
Give each node a column. The bottom view shows, for each column, the last node met in level order, which is the lowest one (on a tie, the later one). Read the columns from left to right.
The problem
Return the nodes visible from below the tree, from the leftmost column to the rightmost. In each column the lowest
node is visible; if two are equally low, the later one in level order. [1, 2, 3, 4, 5, 6, 7] gives
[4, 2, 6, 3, 7]. Trees in the examples are written in level order, with null for a missing child, as on LeetCode.
Think first
- Which node of a column is seen from below?
- How does the top view method change?
- What does the tie rule mean in level order?
Hints
Brute force, and why it fails
Depth-first with (column, depth), keeping the deepest per column.
The key insight
In level order, nodes in a column are met from top to bottom, so the last write is the lowest node.
Dry run
| columns | -2..2 |
|---|
Tree [1, 2, 3, 4, 5, 6, 7]. Columns: 4 at -2, 2 at -1, 1, 5 and 6 at 0, 3 at 1, 7 at 2.
The plan
queue = [(root, 0)]; seen = ordered map
while queue: (n, c) = pop front; seen[c] = n.val; push children
return seen values by column
Complexity
- Time
O(n log n)- Space
O(n)
BFS plus an ordered map.
Edge cases
- Empty tree.
- One node.
- Ties at the same depth: the later one wins.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public List<Integer> bottomView(TreeNode root) {
TreeMap<Integer, Integer> seen = new TreeMap<>(); // column -> the value this view shows
Queue<Object[]> q = new ArrayDeque<>(); // {node, column}, in level order
if (root != null) q.add(new Object[]{root, 0});
while (!q.isEmpty()) {
Object[] item = q.poll();
TreeNode node = (TreeNode) item[0];
int col = (Integer) item[1];
seen.put(col, node.val); // a later (lower) node replaces the earlier one
if (node.left != null) q.add(new Object[]{node.left, col - 1});
if (node.right != null) q.add(new Object[]{node.right, col + 1});
}
return new ArrayList<>(seen.values()); // columns from left to right
}
}C++
class Solution {
public:
vector<int> bottomView(TreeNode* root) {
map<int, int> seen; // column -> the value this view shows
queue<pair<TreeNode*, int>> q; // {node, column}, in level order
if (root != NULL) q.push(make_pair(root, 0));
while (!q.empty()) {
TreeNode* node = q.front().first;
int col = q.front().second;
q.pop();
seen[col] = node->val; // a later (lower) node replaces the earlier one
if (node->left != NULL) q.push(make_pair(node->left, col - 1));
if (node->right != NULL) q.push(make_pair(node->right, col + 1));
}
vector<int> out;
for (map<int, int>::iterator it = seen.begin(); it != seen.end(); ++it) out.push_back(it->second); // left to right
return out;
}
};Python
from collections import deque
class Solution:
def bottomView(self, root) -> list[int]:
seen = {} # column -> the value this view shows
q = deque([(root, 0)] if root else []) # (node, column), in level order
while q:
node, col = q.popleft()
seen[col] = node.val # a later (lower) node replaces the earlier one
if node.left:
q.append((node.left, col - 1))
if node.right:
q.append((node.right, col + 1))
return [seen[c] for c in sorted(seen)] # columns from left to rightCommon mistakes
- Keeping the first value (that is the top view).
- Using preorder, which breaks the tie rule.