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 the 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 1any number of times.
Return theminimum 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: n =7, cost =[1,5,2,2,3,3,1]Output: 6Explanation: We can do the following increments:- Increase the cost of node 4 one time.- Increase the cost of node 3 three times.- Increase the cost of node 7 two times.Each path from the root to a leaf will have a total cost of 9.The total increments we did is1+3+2=6.It can be shown that thisis the minimum answer we can achieve.

Input: n =3, cost =[5,3,3]Output: 0Explanation: The two paths already have equal total costs, so no increments are needed.
To make all root-to-leaf path costs equal in a perfect binary tree, we need to increment node costs so that all paths sum to the same value. We can use postorder DFS to compute the maximum path sum for each subtree and count the increments needed to balance the left and right subtrees at each node.
funcminIncrements(nint, cost []int) int {
ans:=0vardfsfunc(iint) intdfs = func(iint) int {
if2*i+1>=n {
returncost[i]
}
l:=dfs(2*i+1)
r:=dfs(2*i+2)
ifl > r {
ans+=l-r } else {
ans+=r-l }
ifl > r {
returncost[i] +l }
returncost[i] +r }
dfs(0)
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
int ans = 0;
publicintminIncrements(int n, int[] cost) {
dfs(0, n, cost);
return ans;
}
intdfs(int i, int n, int[] cost) {
if (2*i+1 >= n) return cost[i];
int l = dfs(2*i+1, n, cost), r = dfs(2*i+2, n, cost);
ans += Math.abs(l - r);
return cost[i]+ Math.max(l, r);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
var ans = 0funminIncrements(n: Int, cost: IntArray): Int {
fundfs(i: Int): Int {
if (2*i+1>= n) return cost[i]
val l = dfs(2*i+1)
val r = dfs(2*i+2)
ans += kotlin.math.abs(l - r)
return cost[i] + maxOf(l, r)
}
dfs(0)
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defminIncrements(self, n: int, cost: list[int]) -> int:
ans =0defdfs(i: int) -> int:
nonlocal ans
if2*i+1>= n:
return cost[i]
l = dfs(2*i+1)
r = dfs(2*i+2)
ans += abs(l - r)
return cost[i] + max(l, r)
dfs(0)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnmin_increments(n: i32, cost: Vec<i32>) -> i32 {
fndfs(i: usize, n: usize, cost: &Vec<i32>, ans: &muti32) -> i32 {
if2*i+1>= n {
return cost[i];
}
let l = dfs(2*i+1, n, cost, ans);
let r = dfs(2*i+2, n, cost, ans);
*ans += (l - r).abs();
cost[i] + l.max(r)
}
letmut ans =0;
dfs(0, n asusize, &cost, &mut ans);
ans
}
}