Skip to content

Practice Make Costs of Paths Equal in a Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2673
LeetCode Problem

Make Costs of Paths Equal in a Binary Tree

Problem Statement

The perfect binary tree has nodes 1 through n, with children 2i and 2i + 1 when present. cost[i] is the cost of node i + 1. One operation increases any node cost by 1. Return the fewest increments needed so every root-to-leaf path has the same sum of node costs.

Source: Make Costs of Paths Equal in a Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 7, cost = [1,5,2,2,3,3,1]
Output
6

# Example 2

Input
n = 3, cost = [5,3,3]
Output
0

Constraints

3 <= n <= 10^5
n + 1 is a power of 2
cost.length == n
1 <= cost[i] <= 10^4

Related LeetCode Problems

Practice more problems similar to Make Costs of Paths Equal in a Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.