Difference Between Maximum and Minimum Price Sum - Problem

There exists an undirected and initially unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and 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.

Each node has an associated price. You are given an integer array price, where price[i] is the price of the ith node.

The price sum of a given path is the sum of the prices of all nodes lying on that path.

The tree can be rooted at any node root of your choice. The incurred cost after choosing root is the difference between the maximum and minimum price sum amongst all paths starting at root.

Return the maximum possible cost amongst all possible root choices.

Input & Output

Example 1 — Linear Tree
$ Input: n = 3, edges = [[0,1],[1,2]], price = [2,3,1]
Output: 2
💡 Note: When root=0: paths [2+3=5, 2+3+1=6], cost=6-5=1. When root=1: paths [3+2=5, 3+1=4], cost=5-4=1. When root=2: path [1+3+2=6], cost=0. Maximum cost is 2 when we choose an optimal root.
Example 2 — Single Node
$ Input: n = 1, edges = [], price = [5]
Output: 0
💡 Note: Only one node, so there's only one path (the node itself) with sum 5. Max - min = 5 - 5 = 0.
Example 3 — Star Tree
$ Input: n = 4, edges = [[0,1],[0,2],[0,3]], price = [1,2,3,4]
Output: 2
💡 Note: When root=0: paths [1+2=3, 1+3=4, 1+4=5], cost=5-3=2. When root=1: paths [2+1+3=6, 2+1+4=7], cost=7-6=1. When root=2: paths [3+1+2=6, 3+1+4=8], cost=8-6=2. When root=3: paths [4+1+2=7, 4+1+3=8], cost=8-7=1. Maximum cost across all roots is 2.

Constraints

  • 1 ≤ n ≤ 2 × 104
  • edges.length = n - 1
  • 0 ≤ ai, bi ≤ n - 1
  • ai ≠ bi
  • edges represents a valid tree
  • 1 ≤ price[i] ≤ 105

Visualization

Tap to expand
Tree Rerooting Optimization INPUT Undirected Tree (n=3) 0 p=2 1 p=3 2 p=1 n = 3 edges = [[0,1],[1,2]] price = [2, 3, 1] Output: 2 ALGORITHM STEPS 1 Build Adjacency List Create graph from edges 2 First DFS (Root at 0) Compute max path sums 3 Reroot DFS Transfer info to children 4 Compute Max Cost max(maxSum - minSum) Root Analysis Root Paths Max-Min 0 2,5,6 6-2=4 1 5,4 5-4=1 2 1,4,6 6-1=5 Best cost with rerooting: 2 FINAL RESULT Optimal: Root at node 2 2 root 1 0 Path sums from root 2: 2-->1: 1+3 = 4 2-->1-->0: 1+3+2 = 6 Max cost = 6 - 4 = 2 Output: 2 OK Key Insight: Tree Rerooting allows computing answers for all root choices in O(n) time instead of O(n^2). First DFS computes subtree info, second DFS transfers parent contribution to children efficiently. For each root, find max path sum (including root) minus min path sum to get the cost difference. TutorialsPoint - Difference Between Maximum and Minimum Price Sum | Tree Rerooting Optimization
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
8.5K Views
Medium Frequency
~35 min Avg. Time
189 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