Skip to content

Practice Most Profitable Path in a Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2467
LeetCode Problem

Most Profitable Path in a Tree

Problem Statement

edges describes an undirected tree rooted at 0, and amount[i] is an even signed gate payment: negative costs money and nonnegative grants a reward. Alice starts at 0 and chooses a path to a leaf; Bob starts at bob and follows the path to 0. Each moves one edge per second and stops at their destination. Only the first arrival receives or pays the gate amount; simultaneous arrivals split it equally. Return the maximum net income Alice can achieve.

Source: Most Profitable Path in a Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
Output
6

# Example 2

Input
edges = [[0,1]], bob = 1, amount = [-7280,2350]
Output
-7280

Constraints

2 <= n <= 10^5
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
ai != bi
edges represents a valid tree.
1 <= bob < n
amount.length == n
amount[i] is an even integer in the range [-10^4, 10^4].

Related LeetCode Problems

Practice more problems similar to Most Profitable Path in a Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.