Problem Description
Given a perfect binary tree with n nodes (numbered from 1 to n) and a 0-indexed cost array where cost[i] represents the cost of node i+1, you are allowed to increment the cost of any node by 1 any number of times. The goal is to perform the minimum number of increments such that all paths from the root to each leaf have the same total cost.
Key Insights
- The perfect binary tree structure (each non-leaf node has exactly 2 children) allows a neat DFS/post-order traversal.
- For each non-leaf node, the cost discrepancies between its two children's root-to-leaf paths must be balanced.
- By incrementing the subtree path that has a lower sum, you can make both child paths equal.
- A bottom-up traversal lets you propagate the balanced path values upward, simultaneously keeping track of the number of increments needed.
Space and Time Complexity
Time Complexity: O(n) – We visit each node once. Space Complexity: O(n) in the worst-case scenario due to recursion stack space (O(log n) for a balanced tree).
Solution
We use a DFS (post-order) approach starting at the root. For leaf nodes, simply return the node’s cost. For non-leaf nodes, recursively compute the balanced path sums for the left and right children. At each node:
- Calculate the increments required to balance the two children’s sums by taking the difference.
- Update a global counter with these increments.
- Return the sum of the node’s own cost and the maximum of its two balanced child path sums, so that the subtree from this node is balanced. This approach guarantees minimal increments as we always only "fill the gap" on the lower cost path.