Minimum Increments to Equalize Leaf Paths - Problem
Tree Path Balancing Challenge
Imagine you're a park designer tasked with creating a fair hiking trail system! You have a tree-structured network of trails rooted at the main entrance (node 0), where each trail segment has a difficulty cost.
The problem: hikers complain that some paths from the entrance to scenic viewpoints (leaf nodes) are much easier than others. Your job is to increase the difficulty of certain trail segments so that every path from the entrance to any viewpoint has the same total difficulty score.
You can only increase the cost of trail segments (you can't make them easier), and you want to modify the minimum number of segments possible to keep costs down.
Goal: Return the minimum number of nodes whose cost must be increased to make all root-to-leaf path scores equal.
Imagine you're a park designer tasked with creating a fair hiking trail system! You have a tree-structured network of trails rooted at the main entrance (node 0), where each trail segment has a difficulty cost.
The problem: hikers complain that some paths from the entrance to scenic viewpoints (leaf nodes) are much easier than others. Your job is to increase the difficulty of certain trail segments so that every path from the entrance to any viewpoint has the same total difficulty score.
You can only increase the cost of trail segments (you can't make them easier), and you want to modify the minimum number of segments possible to keep costs down.
Goal: Return the minimum number of nodes whose cost must be increased to make all root-to-leaf path scores equal.
Example: If one path has score 10 and another has score 15, you need to increase costs along the first path by 5 to equalize them. Input & Output
example_1.py โ Basic Tree
$
Input:
n = 6, edges = [[0,1],[0,2],[1,3],[1,4],[2,5]], cost = [5,3,4,2,6,1]
โบ
Output:
8
๐ก Note:
Tree has paths: 0โ1โ3 (sum=10), 0โ1โ4 (sum=14), 0โ2โ5 (sum=10). To equalize all paths to sum 14, we need 8 total increments: 4 increments for path 0โ1โ3 and 4 increments for path 0โ2โ5.
example_2.py โ Already Balanced
$
Input:
n = 3, edges = [[0,1],[0,2]], cost = [1,2,2]
โบ
Output:
0
๐ก Note:
Both paths 0โ1 (sum=3) and 0โ2 (sum=3) already have equal sums, so no increments are needed.
example_3.py โ Single Node
$
Input:
n = 1, edges = [], cost = [5]
โบ
Output:
0
๐ก Note:
Single node tree has only one 'path' with sum=5. No other paths to balance, so 0 increments needed.
Visualization
Tap to expand
Understanding the Visualization
1
Build Tree Structure
Convert edge list into adjacency list representation for easy traversal
2
DFS from Root
Start depth-first search from root node, processing children before parents
3
Calculate Child Maximums
For each internal node, find the maximum path sum among all child subtrees
4
Balance Subtrees
Add increments equal to the difference between max child sum and other child sums
5
Return Balanced Sum
Each node returns its cost plus the balanced maximum child sum upward
Key Takeaway
๐ฏ Key Insight: By processing the tree bottom-up, we can balance all subtrees locally at each internal node, ensuring global path equality with minimum increments in just one traversal!
Time & Space Complexity
Time Complexity
O(n)
Single DFS traversal visits each node exactly once
โ Linear Growth
Space Complexity
O(h)
Recursion stack depth equals tree height h, which is O(log n) for balanced trees
โ Linear Space
Constraints
- 1 โค n โค 105
- edges.length == n - 1
- 0 โค ui, vi < n
- 1 โค cost[i] โค 104
- The input represents a valid tree (connected and acyclic)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code