---
# System prepended metadata

title: 2673. Make Costs of Paths Equal in a Binary Tree
tags: [Leetcode, Tree, DFS, Medium]

---

Link: https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/description/
## 思路
Bottom up DFS
cost[i]存当前node到leaf node的cost
Follow up Question: 如果既可以加也可以减 (https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree/solutions/3494915/java-c-python-bottom-up-and-follow-up/)

## Code
```python=
class Solution {
    public int minIncrements(int n, int[] cost) {
        int res = 0;
        for(int i=n/2-1; i>=0; i--){
            int l = 2*i+1, r = 2*i+2;
            res += Math.abs(cost[l]-cost[r]);
            cost[i] += Math.max(cost[l], cost[r]);
        }
        return res;
    }
}
```