Skip to content

Commit 60ad101

Browse files
committed
feat: add solutions to lc problem: No.0979
No.0979.Distribute Coins in Binary Tree
1 parent 659f04c commit 60ad101

File tree

7 files changed

+151
-59
lines changed

7 files changed

+151
-59
lines changed

solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md

+58-22
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64-
先遍历左右子树,获得硬币的余额。比如,我们从左子树得到 `left = "+3"`,说明左子树有 3 个额外的硬币需要移出。如果我们从右子树得到 `right = "-1"`,说明右子树需要额外移入 1 个硬币。累加移动的次数 `abs(+3) + abs(-1)`,然后返回整个二叉树的余额 `left + right + root.val - 1`
64+
**方法一:DFS**
6565

66-
返回最终的移动次数即可。
66+
我们定义一个函数 $dfs(node)$,表示以 $node$ 为根节点的子树中,金币的超载量,即金币的数量减去节点数。如果 $dfs(node)$ 为正数,表示该子树中金币的数量多于节点数,需要将多余的金币移出该子树;如果 $dfs(node)$ 为负数,表示该子树中金币的数量少于节点数,需要将不足的金币移入该子树。
67+
68+
在函数 $dfs(node)$ 中,我们首先遍历左右子树,获得左右子树的金币超载量 $left$ 和 $right$。那么当前移动的次数需要加上 $|left| + |right|$,即将左右子树中的金币移动到当前节点。然后,我们返回整个子树的金币超载量,即 $left + right + node.val - 1$。
69+
70+
最后返回移动的次数即可。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(h)$。其中 $n$ 和 $h$ 分别是二叉树的节点数和高度。
6773

6874
<!-- tabs:start -->
6975

@@ -79,12 +85,12 @@
7985
# self.left = left
8086
# self.right = right
8187
class Solution:
82-
def distributeCoins(self, root: TreeNode) -> int:
88+
def distributeCoins(self, root: Optional[TreeNode]) -> int:
8389
def dfs(root):
84-
nonlocal ans
8590
if root is None:
8691
return 0
8792
left, right = dfs(root.left), dfs(root.right)
93+
nonlocal ans
8894
ans += abs(left) + abs(right)
8995
return left + right + root.val - 1
9096

@@ -114,11 +120,9 @@ class Solution:
114120
* }
115121
*/
116122
class Solution {
117-
118123
private int ans;
119124

120125
public int distributeCoins(TreeNode root) {
121-
ans = 0;
122126
dfs(root);
123127
return ans;
124128
}
@@ -151,26 +155,26 @@ class Solution {
151155
*/
152156
class Solution {
153157
public:
154-
int ans;
155-
156158
int distributeCoins(TreeNode* root) {
157-
ans = 0;
159+
int ans = 0;
160+
function<int(TreeNode*)> dfs = [&](TreeNode* root) -> int {
161+
if (!root) {
162+
return 0;
163+
}
164+
int left = dfs(root->left);
165+
int right = dfs(root->right);
166+
ans += abs(left) + abs(right);
167+
return left + right + root->val - 1;
168+
};
158169
dfs(root);
159170
return ans;
160171
}
161-
162-
int dfs(TreeNode* root) {
163-
if (!root) return 0;
164-
int left = dfs(root->left), right = dfs(root->right);
165-
ans += abs(left) + abs(right);
166-
return left + right + root->val - 1;
167-
}
168172
};
169173
```
170174
171-
### **C++**
175+
### **Go**
172176
173-
```cpp
177+
```go
174178
/**
175179
* Definition for a binary tree node.
176180
* type TreeNode struct {
@@ -179,9 +183,8 @@ public:
179183
* Right *TreeNode
180184
* }
181185
*/
182-
func distributeCoins(root *TreeNode) int {
183-
ans := 0
184-
var dfs func(root *TreeNode) int
186+
func distributeCoins(root *TreeNode) (ans int) {
187+
var dfs func(*TreeNode) int
185188
dfs = func(root *TreeNode) int {
186189
if root == nil {
187190
return 0
@@ -191,7 +194,7 @@ func distributeCoins(root *TreeNode) int {
191194
return left + right + root.Val - 1
192195
}
193196
dfs(root)
194-
return ans
197+
return
195198
}
196199
197200
func abs(x int) int {
@@ -202,6 +205,39 @@ func abs(x int) int {
202205
}
203206
```
204207

208+
### **TypeScript**
209+
210+
```ts
211+
/**
212+
* Definition for a binary tree node.
213+
* class TreeNode {
214+
* val: number
215+
* left: TreeNode | null
216+
* right: TreeNode | null
217+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
218+
* this.val = (val===undefined ? 0 : val)
219+
* this.left = (left===undefined ? null : left)
220+
* this.right = (right===undefined ? null : right)
221+
* }
222+
* }
223+
*/
224+
225+
function distributeCoins(root: TreeNode | null): number {
226+
let ans = 0;
227+
const dfs = (root: TreeNode | null) => {
228+
if (!root) {
229+
return 0;
230+
}
231+
const left = dfs(root.left);
232+
const right = dfs(root.right);
233+
ans += Math.abs(left) + Math.abs(right);
234+
return left + right + root.val - 1;
235+
};
236+
dfs(root);
237+
return ans;
238+
}
239+
```
240+
205241
### **...**
206242

207243
```

solution/0900-0999/0979.Distribute Coins in Binary Tree/README_EN.md

+50-20
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
# self.left = left
5252
# self.right = right
5353
class Solution:
54-
def distributeCoins(self, root: TreeNode) -> int:
54+
def distributeCoins(self, root: Optional[TreeNode]) -> int:
5555
def dfs(root):
56-
nonlocal ans
5756
if root is None:
5857
return 0
5958
left, right = dfs(root.left), dfs(root.right)
59+
nonlocal ans
6060
ans += abs(left) + abs(right)
6161
return left + right + root.val - 1
6262

@@ -84,11 +84,9 @@ class Solution:
8484
* }
8585
*/
8686
class Solution {
87-
8887
private int ans;
8988

9089
public int distributeCoins(TreeNode root) {
91-
ans = 0;
9290
dfs(root);
9391
return ans;
9492
}
@@ -121,26 +119,26 @@ class Solution {
121119
*/
122120
class Solution {
123121
public:
124-
int ans;
125-
126122
int distributeCoins(TreeNode* root) {
127-
ans = 0;
123+
int ans = 0;
124+
function<int(TreeNode*)> dfs = [&](TreeNode* root) -> int {
125+
if (!root) {
126+
return 0;
127+
}
128+
int left = dfs(root->left);
129+
int right = dfs(root->right);
130+
ans += abs(left) + abs(right);
131+
return left + right + root->val - 1;
132+
};
128133
dfs(root);
129134
return ans;
130135
}
131-
132-
int dfs(TreeNode* root) {
133-
if (!root) return 0;
134-
int left = dfs(root->left), right = dfs(root->right);
135-
ans += abs(left) + abs(right);
136-
return left + right + root->val - 1;
137-
}
138136
};
139137
```
140138
141-
### **C++**
139+
### **Go**
142140
143-
```cpp
141+
```go
144142
/**
145143
* Definition for a binary tree node.
146144
* type TreeNode struct {
@@ -149,9 +147,8 @@ public:
149147
* Right *TreeNode
150148
* }
151149
*/
152-
func distributeCoins(root *TreeNode) int {
153-
ans := 0
154-
var dfs func(root *TreeNode) int
150+
func distributeCoins(root *TreeNode) (ans int) {
151+
var dfs func(*TreeNode) int
155152
dfs = func(root *TreeNode) int {
156153
if root == nil {
157154
return 0
@@ -161,7 +158,7 @@ func distributeCoins(root *TreeNode) int {
161158
return left + right + root.Val - 1
162159
}
163160
dfs(root)
164-
return ans
161+
return
165162
}
166163
167164
func abs(x int) int {
@@ -172,6 +169,39 @@ func abs(x int) int {
172169
}
173170
```
174171

172+
### **TypeScript**
173+
174+
```ts
175+
/**
176+
* Definition for a binary tree node.
177+
* class TreeNode {
178+
* val: number
179+
* left: TreeNode | null
180+
* right: TreeNode | null
181+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
182+
* this.val = (val===undefined ? 0 : val)
183+
* this.left = (left===undefined ? null : left)
184+
* this.right = (right===undefined ? null : right)
185+
* }
186+
* }
187+
*/
188+
189+
function distributeCoins(root: TreeNode | null): number {
190+
let ans = 0;
191+
const dfs = (root: TreeNode | null) => {
192+
if (!root) {
193+
return 0;
194+
}
195+
const left = dfs(root.left);
196+
const right = dfs(root.right);
197+
ans += Math.abs(left) + Math.abs(right);
198+
return left + right + root.val - 1;
199+
};
200+
dfs(root);
201+
return ans;
202+
}
203+
```
204+
175205
### **...**
176206

177207
```

solution/0900-0999/0979.Distribute Coins in Binary Tree/Solution.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
*/
1212
class Solution {
1313
public:
14-
int ans;
15-
1614
int distributeCoins(TreeNode* root) {
17-
ans = 0;
15+
int ans = 0;
16+
function<int(TreeNode*)> dfs = [&](TreeNode* root) -> int {
17+
if (!root) {
18+
return 0;
19+
}
20+
int left = dfs(root->left);
21+
int right = dfs(root->right);
22+
ans += abs(left) + abs(right);
23+
return left + right + root->val - 1;
24+
};
1825
dfs(root);
1926
return ans;
2027
}
21-
22-
int dfs(TreeNode* root) {
23-
if (!root) return 0;
24-
int left = dfs(root->left), right = dfs(root->right);
25-
ans += abs(left) + abs(right);
26-
return left + right + root->val - 1;
27-
}
2828
};

solution/0900-0999/0979.Distribute Coins in Binary Tree/Solution.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* Right *TreeNode
77
* }
88
*/
9-
func distributeCoins(root *TreeNode) int {
10-
ans := 0
11-
var dfs func(root *TreeNode) int
9+
func distributeCoins(root *TreeNode) (ans int) {
10+
var dfs func(*TreeNode) int
1211
dfs = func(root *TreeNode) int {
1312
if root == nil {
1413
return 0
@@ -18,7 +17,7 @@ func distributeCoins(root *TreeNode) int {
1817
return left + right + root.Val - 1
1918
}
2019
dfs(root)
21-
return ans
20+
return
2221
}
2322

2423
func abs(x int) int {

solution/0900-0999/0979.Distribute Coins in Binary Tree/Solution.java

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Solution {
1717
private int ans;
1818

1919
public int distributeCoins(TreeNode root) {
20-
ans = 0;
2120
dfs(root);
2221
return ans;
2322
}

solution/0900-0999/0979.Distribute Coins in Binary Tree/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def distributeCoins(self, root: TreeNode) -> int:
8+
def distributeCoins(self, root: Optional[TreeNode]) -> int:
99
def dfs(root):
10-
nonlocal ans
1110
if root is None:
1211
return 0
1312
left, right = dfs(root.left), dfs(root.right)
13+
nonlocal ans
1414
ans += abs(left) + abs(right)
1515
return left + right + root.val - 1
1616

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function distributeCoins(root: TreeNode | null): number {
16+
let ans = 0;
17+
const dfs = (root: TreeNode | null) => {
18+
if (!root) {
19+
return 0;
20+
}
21+
const left = dfs(root.left);
22+
const right = dfs(root.right);
23+
ans += Math.abs(left) + Math.abs(right);
24+
return left + right + root.val - 1;
25+
};
26+
dfs(root);
27+
return ans;
28+
}

0 commit comments

Comments
 (0)