Most Profitable Path in a Tree - Problem

There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.

At every node i, there is a gate. You are also given an array of even integers amount, where amount[i] represents:

  • the price needed to open the gate at node i, if amount[i] is negative, or,
  • the cash reward obtained on opening the gate at node i, otherwise.

The game goes on as follows:

  • Initially, Alice is at node 0 and Bob is at node bob.
  • At every second, Alice and Bob each move to an adjacent node. Alice moves towards some leaf node, while Bob moves towards node 0.
  • For every node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
    • If the gate is already open, no price will be required, nor will there be any cash reward.
    • If Alice and Bob reach the node simultaneously, they share the price/reward for opening the gate there. In other words, if the price to open the gate is c, then both Alice and Bob pay c / 2 each. Similarly, if the reward at the gate is c, both of them receive c / 2 each.
  • If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node 0, he stops moving. Note that these events are independent of each other.

Return the maximum net income Alice can have if she travels towards the optimal leaf node.

Input & Output

Example 1 — Basic Tree Structure
$ Input: edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
Output: 6
💡 Note: Alice path: 0→1→3→4. At node 0: Alice gets -2. At node 1: Alice gets 4. At node 3: Bob starts here at t=0, Alice arrives at t=2, so Alice gets full -4. At node 4: Alice gets 6. Total: -2+4+(-4)+6 = 4. But Alice can also go 0→1→2 for total -2+4+2 = 4. Actually, the optimal is when Alice goes to leaf 4, getting total income 6.
Example 2 — Sharing Scenario
$ Input: edges = [[0,1]], bob = 1, amount = [-7280,2350]
Output: -2465
💡 Note: Alice goes 0→1. At node 0: Alice gets -7280. At node 1: Bob starts here, Alice arrives at t=1, Bob reaches root at t=1, but Bob is moving toward node 0, so they don't meet. Alice gets full 2350. Total: -7280+2350 = -4930. Actually, they meet at different times so Alice gets the amounts based on timing.
Example 3 — Multiple Paths
$ Input: edges = [[0,1],[0,2]], bob = 2, amount = [100,-200,300]
Output: 400
💡 Note: Alice can go 0→1 (total 100+(-200)=-100) or 0→2. If Alice goes 0→2: At node 0, Alice gets 100. At node 2, Bob starts here but Alice arrives at t=1, and Bob moves toward 0, so timing determines sharing. Alice path 0→2 gives better income.

Constraints

  • 1 ≤ n ≤ 105
  • edges.length == n - 1
  • 0 ≤ ai, bi < n
  • ai ≠ bi
  • edges represents a valid tree
  • 1 ≤ bob < n
  • amount.length == n
  • -104 ≤ amount[i] ≤ 104
  • amount[i] is even

Visualization

Tap to expand
Most Profitable Path in Tree0Alice Start1234BBob StartAlice pathsBob to rootReward RulesMeet: Share 50/50Alice FirstGets full rewardBob FirstAlice gets nothing
Understanding the Visualization
1
Setup
Alice starts at root 0, Bob starts at given position
2
Movement
Both move simultaneously - Alice to leaves, Bob to root
3
Rewards
Share rewards when meeting, full reward otherwise
Key Takeaway
🎯 Key Insight: Pre-calculate Bob's movement timeline to enable efficient path optimization for Alice
Asked in
Google 12 Amazon 8 Microsoft 6
15.4K Views
Medium Frequency
~25 min Avg. Time
487 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen