You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2600-2699/2673.Make Costs of Paths Equal in a Binary Tree/README_EN.md
+45-56
Original file line number
Diff line number
Diff line change
@@ -54,47 +54,43 @@ It can be shown that this is the minimum answer we can achieve.
54
54
55
55
## Solutions
56
56
57
-
### Solution 1
57
+
### Solution 1: Greedy Algorithm
58
+
59
+
According to the problem description, we need to calculate the minimum number of increments to make the path values from the root node to each leaf node equal.
60
+
61
+
The path values from the root node to each leaf node being equal is actually equivalent to the path values from any node as the root of a subtree to each leaf node of that subtree being equal.
62
+
63
+
Why is that? We can prove it by contradiction. Suppose there is a node $x$, and the path values from it as the root of a subtree to some leaf nodes are not equal. Then there exists a situation where the path values from the root node to the leaf nodes are not equal, which contradicts the condition "the path values from the root node to each leaf node are equal". Therefore, the assumption is not valid, and the path values from any node as the root of a subtree to each leaf node of that subtree are equal.
64
+
65
+
We can start from the bottom of the tree and calculate the number of increments layer by layer. For each non-leaf node, we can calculate the path values of its left and right child nodes. The number of increments is the difference between the two path values, and then update the path values of the left and right child nodes to the larger one of the two.
66
+
67
+
Finally, return the total number of increments.
68
+
69
+
The time complexity is $O(n)$, where $n$ is the number of nodes. The space complexity is $O(1)$.
0 commit comments