Problem Statement (English original)
Assume a binary-tree node:
class Node {
int val;
Node left, right;
}
Given the root of a binary tree, output the node values in vertical order (column order) from leftmost column to rightmost column.
Within each column, list nodes top-to-bottom in the order they are encountered by a standard BFS (or by row index if you compute coordinates).
Illustrative example (matching the screenshot tree):
6
/ \
3 4
/ \ \
5 1 0
\ / /
2 8 7
/ \
9 7 (shape illustrative)
Expected vertical-order output:
[5, 9, 3, 2, 6, 1, 7, 4, 8, 0]
(Columns from left to right:
-2: [5]
-1: [9, 3, 2]
0: [6, 1, 7]
+1: [4, 8]
+2: [0])
中文问题(等价表述)
给定二叉树根节点,按 列从最左列到最右列输出结点值;同一列内按 自上而下 顺序输出。上图示例的输出为:[5, 9, 3, 2, 6, 1, 7, 4, 8, 0]。
简要总结 / 思路(中)
用 BFS + 坐标:根列号 0,左子列号 -1,右子列号 +1。层序遍历,把每个结点加入 col -> list 的哈希表,同时更新 minCol/maxCol。最后按列从 minCol 到 maxCol 拼接。时间 O(n),空间 O(n)。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 OpenAI、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。