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.

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
Tree Path Balancing Visualization0(5)1(3)2(4)3(2)4(6)5(1)Processing Steps:1. Leaves: 3โ†’2, 4โ†’6, 5โ†’12. Node 1: max(2,6)=6, +4 increments3. Node 2: child=1, no balancing4. Root: max(9,5)=9, +4 incrementsTotal: 8 incrementsFinal Path Sums:Path 0โ†’1โ†’3: 5+3+2 = 10 (+4) = 14Path 0โ†’1โ†’4: 5+3+6 = 14 (no change)Path 0โ†’2โ†’5: 5+4+1 = 10 (+4) = 14โœ“ All paths balanced at sum = 14
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

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

Recursion stack depth equals tree height h, which is O(log n) for balanced trees

n
2n
โœ“ 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)
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22
42.8K Views
Medium Frequency
~25 min Avg. Time
1.1K 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