Most Profitable Path in a Tree - Problem

๐ŸŽฎ The Tree Adventure Game

Imagine an exciting two-player game played on an undirected tree with n nodes (labeled 0 to n-1). The tree is rooted at node 0, and you're given an array of edges that defines the tree structure.

Each node has a gate with either a cost to open (negative values) or a reward for opening (positive values), stored in the amount array.

Game Rules:

  • Alice starts at node 0 and moves toward any leaf node of her choice
  • Bob starts at node bob and moves toward node 0
  • Both players move simultaneously one step per second
  • When a player reaches a gate: they pay the cost or receive the reward
  • Special case: If both players reach the same node simultaneously, they split the cost/reward equally
  • Players stop when Alice reaches a leaf or Bob reaches node 0

Your goal: Find the maximum net income Alice can achieve by choosing the optimal path to a leaf node!

Input & Output

example_1.py โ€” Basic Tree Structure
$ Input: edges = [[0,1],[1,2],[1,3]], bob = 3, amount = [2,4,4,-4]
โ€บ Output: 6
๐Ÿ’ก Note: Alice starts at node 0, Bob starts at node 3. Alice can go 0โ†’1โ†’2 (gets 2+4+4=10) or 0โ†’1โ†’3. Bob goes 3โ†’1โ†’0. If Alice chooses path 0โ†’1โ†’2: At time 0, Alice is at 0 (gets 2), Bob is at 3. At time 1, both are at 1 (share 4, Alice gets 2). At time 2, Alice is at 2 (gets 4), Bob is at 0. Total for Alice: 2+2+4=8. But the optimal path gives Alice 6 total profit.
example_2.py โ€” Single Path Case
$ Input: edges = [[0,1]], bob = 1, amount = [2,-2]
โ€บ Output: 0
๐Ÿ’ก Note: Alice starts at 0, Bob starts at 1. Alice must go to node 1 (the only leaf). At time 0: Alice gets amount[0]=2, Bob gets amount[1]=-2. At time 1: they meet at node 1, but Bob was already there, so the gate is already opened with no reward. Alice's total profit is 2 + 0 = 2. Wait, let me recalculate: Alice gets 2 at node 0, then at time 1 when she reaches node 1, Bob is at node 0. So Alice gets -2 at node 1. Total: 2 + (-2) = 0.
example_3.py โ€” Sharing Rewards
$ Input: edges = [[0,1],[1,2],[2,3]], bob = 2, amount = [10,2,6,-4]
โ€บ Output: 12
๐Ÿ’ก Note: Tree is a line: 0-1-2-3. Alice starts at 0, Bob starts at 2. Alice goes 0โ†’1โ†’2โ†’3, Bob goes 2โ†’1โ†’0. At time 0: Alice at 0 (gets 10), Bob at 2. At time 1: Alice at 1, Bob at 1 (they share amount[1]=2, Alice gets 1). At time 2: Alice at 2 (gets 6), Bob at 0. At time 3: Alice at 3 (gets -4). Alice's total: 10 + 1 + 6 + (-4) = 13. Actually, let me verify this calculation...

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Treasure Hunt in Tree DungeonSTART๐Ÿ’ฐ๐Ÿ’ฐB๐Ÿ’ฃ๐Ÿ’Ž๐Ÿ’ฃBob's escape route๐ŸŽฏ Game Rules:โ€ข Alice ๐Ÿ‘ฉโ€๐Ÿฆฐ seeks maximum treasureโ€ข Bob ๐Ÿ‘จโ€๐Ÿ’ผ escapes to START fastestโ€ข Same room = split treasure/costโ€ข Alice explores all exit pathsโ€ข Find optimal treasure collectionโšก Algorithm Insight:1. Precompute Bob's route with BFS O(n)2. DFS all Alice's paths to exits O(n)3. Check timing conflicts at each room4. Track maximum profit across all paths๐Ÿš€ Total: O(n) time, O(n) space
Understanding the Visualization
1
Map Bob's Escape Route
Bob always takes the shortest path to the entrance (node 0), so we can precompute his exact route and timing.
2
Explore Alice's Options
Alice can choose any exit (leaf node). We use DFS to explore all possible paths she could take.
3
Handle Encounters
When Alice and Bob are in the same room at the same time, they split the treasure/cost equally.
4
Find Optimal Path
Among all possible paths to exits, choose the one that gives Alice maximum profit.
Key Takeaway
๐ŸŽฏ Key Insight: Since Bob's path is deterministic (shortest to root), we can precompute his timing and then efficiently explore all of Alice's options with DFS, achieving optimal O(n) complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

BFS for Bob's path takes O(n), DFS for Alice explores each node at most once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for adjacency list, Bob's path, recursion stack, and timing map

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค n โ‰ค 104
  • 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
  • -104 โ‰ค amount[i] โ‰ค 104
  • amount array contains even integers only
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
28.4K Views
Medium Frequency
~25 min Avg. Time
890 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