Make Costs of Paths Equal in a Binary Tree - Problem

You are given an integer n representing the number of nodes in a perfect binary tree consisting of nodes numbered from 1 to n. The root of the tree is node 1 and each node i in the tree has two children where the left child is node 2 * i and the right child is 2 * i + 1.

Each node in the tree also has a cost represented by a given 0-indexed integer array cost of size n where cost[i] is the cost of node i + 1. You are allowed to increment the cost of any node by 1 any number of times.

Return the minimum number of increments you need to make the cost of paths from the root to each leaf node equal.

Note:

  • A perfect binary tree is a tree where each node, except the leaf nodes, has exactly 2 children.
  • The cost of a path is the sum of costs of nodes in the path.

Input & Output

Example 1 — Basic Perfect Binary Tree
$ Input: cost = [1,5,2,2,3,3,1]
Output: 6
💡 Note: Tree has paths: 1→5→2 (cost 8), 1→5→3 (cost 9), 1→2→3 (cost 6), 1→2→1 (cost 4). To balance, increment path costs to max 9: need 1+0+3+5 = 9 increments, but optimal is 6.
Example 2 — Small Tree
$ Input: cost = [7,7,7]
Output: 0
💡 Note: Tree paths: 7→7 (cost 14) and 7→7 (cost 14). Both paths already have equal costs, so no increments needed.
Example 3 — Single Node
$ Input: cost = [5]
Output: 0
💡 Note: Only one node, so only one path with cost 5. No balancing needed.

Constraints

  • n == cost.length
  • 3 ≤ n ≤ 15
  • cost.length is odd
  • 1 ≤ cost[i] ≤ 104

Visualization

Tap to expand
Make Costs of Paths Equal in a Binary Tree INPUT Perfect Binary Tree (n=7) node 1 cost=1 node 2 cost=5 node 3 cost=2 node 4 cost=2 node 5 cost=3 node 6 cost=3 node 7 cost=1 cost array (0-indexed): [1, 5, 2, 2, 3, 3, 1] Current path costs: 1-2-4: 8 | 1-2-5: 9 1-3-6: 6 | 1-3-7: 4 ALGORITHM STEPS DFS Bottom-Up Balancing 1 Start from leaves Process nodes 4,5,6,7 Get subtree costs 2 Balance siblings Add to smaller child to match larger sibling 3 Propagate up Each node returns max subtree cost to parent 4 Sum increments Total = |left - right| for all internal nodes Balancing Calculations: Node 2: |2-3| = 1 incr Node 3: |3-1| = 2 incr Node 1: |(5+3)-(2+5)| = 1 Node 3 subtree: +2 more Total: 1+2+1+2 = 6 FINAL RESULT Balanced Tree (all paths = 9) 1 5 5 +3 3 +1 3 3 3 +2 All Path Costs = 9 Path 1-2-4: 1+5+3 = 9 OK Path 1-2-5: 1+5+3 = 9 OK Path 1-3-6: 1+5+3 = 9 OK OUTPUT 6 Key Insight: Bottom-up balancing ensures optimal increments. At each internal node, we balance left and right subtrees by incrementing the smaller one. The difference |left - right| gives the minimum increments needed. This greedy approach works because once balanced, any increment to a subtree must be applied equally to all its leaves. TutorialsPoint - Make Costs of Paths Equal in a Binary Tree | DFS Bottom-Up Balancing Time Complexity: O(n) | Space Complexity: O(h) where h is tree height
Asked in
Meta 15 Amazon 12 Google 10 Microsoft 8
28.4K Views
Medium Frequency
~25 min Avg. Time
856 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