← 返回 meta 的题目列表Binary Tree Vertical Order / Right Side View
类型:qbank
LeetCode 314 (Vertical Order Traversal) and LeetCode 199 (Right Side View). Phone-screen staple, frequently asked as a paired combo (left view + right view) or as the vertical-order traversal with deterministic ordering.
Requirements
Vertical Order: BFS by level, attach a column index (col), bucket by column, output columns left-to-right. Same-cell ordering: top-down, then left-to-right by insertion order (some variants ask for value-sorted within a cell — clarify).
Right Side View: BFS by level, take the last node of each level; or DFS right-first, recording first node per depth.
Common combo follow-up: return both left view and right view from the same BFS pass.
Examples
For [1, 2, 3, 4, 5, 6, 7], right view = [1, 3, 7]; vertical order columns: [-2:[4], -1:[2], 0:[1,5,6], 1:[3], 2:[7]].
Notes
BFS with a (node, col) queue is the consensus implementation. DFS works but needs explicit min_col / max_col tracking.
Watch the LeetCode 987 variant (Vertical Order Traversal II) where same-cell values must be sorted — confirm which version the interviewer wants.
Preparation
Write BFS vertical-order in <10 min: queue of (node, col), defaultdict(list), sort by col at the end.
Practice right-view two ways (BFS last-of-level, DFS right-first) so you can pivot when the interviewer asks for the alternative.
Drill the left+right combo variant — it shows up repeatedly in the same Meta phone screen.