Given a binary tree, imagine yourself standing on the left side of it,
return the values of the nodes you can see ordered from bottom to top,
then switch to right side of the tree, and return the values of the nodes you can see ordered from top to bottom.
Binary Tree:
1
/ \
2 3
/ \ / \
6 5 5 4
Answer: [6, 2, 1, 3, 4]
Binary Tree:
1
/ \
2 3
\
5
Answer: [5, 2, 1, 3, 5]
站在左侧时,能看到每层最左节点,并且结果从底到顶,需要将层序遍历的左视图反转。站在右侧时,能看到每层最右节点,从顶到底依序加入即可。整题的核心技巧是 BFS 层序遍历,从每层中提取左端或右端的可见节点,分别按要求顺序输出。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Meta、Google、Amazon 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。
正文完