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 a Tree INPUT 0 -2 1 +4 2 +2 3 -4 Bob Start 4 +6 edges=[[0,1],[1,2],[1,3],[3,4]] bob = 3 amount = [-2,4,2,-4,6] ALGORITHM STEPS 1 Build Tree Graph Create adjacency list 2 Find Bob's Path Path: 3 --> 1 --> 0 bobTime[3]=0, bobTime[1]=1 bobTime[0]=2 Bob reaches each node at time t 3 DFS from Node 0 Track Alice's income to leaves 4 Calculate Income Compare arrival times If Alice arrives first: income += amount[node] If same time: income += amount[node]/2 If Bob first: income += 0 FINAL RESULT Optimal Path: 0 --> 1 --> 3 --> 4 Node 0 (t=0): Bob at t=2, Alice first income = -2 Node 1 (t=1): Bob at t=1, same time income = 4/2 = 2 Node 3 (t=2): Bob at t=0, Bob first income = 0 Node 4 (t=3) - Leaf: Bob never visits income = 6 Total Income: -2 + 2 + 0 + 6 = 6 Output: 6 Key Insight: Pre-compute Bob's path and arrival times first. Then DFS from Alice's start (node 0) to find the maximum income path to any leaf. Compare arrival times at each node to determine if Alice gets full amount, half amount, or nothing. Time complexity: O(n) for both BFS/DFS traversals. TutorialsPoint - Most Profitable Path in a Tree | Optimized DFS with Pre-computed Bob Path
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