DoorDash VO 面试题解析:二叉树中两“存活”节点间的最大路径和(树形 DP / 后序 DFS)

47次阅读
没有评论

Part A — Leaves are the only“alive”nodes

You are given a binary tree whose nodes have integer values.
Define an alive node as a leaf node.
Find the maximum path sum between any two alive nodes in the tree.
A path is a sequence of connected nodes; its sum is the sum of node values along the path.

Return the maximum possible sum among all paths whose two endpoints are alive nodes.


Part B (Follow-up) — Internal nodes can also be“alive”

Now some non-leaf nodes may also be marked as alive (e.g., denoted by an asterisk in the example).
Find the maximum path sum between any two alive nodes subject to the constraint:

  • Along the chosen path, the only alive nodes are its two endpoints (i.e., no other alive node is allowed in between).

Return the maximum possible sum under this rule.

Example (informal):
If the alive nodes are {100*, 2*, 50*, 15*, 4*} and the best path is 100* → 2* → 50*, the answer is 100 + 2 + 50 = 152.

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Doordash、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Tiktok 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0