Skip to content

Commit 0fb490f

Browse files
committed
feat: add solutions to lc problem: No.0979
No.0979.Distribute Coins in Binary Tree
1 parent 0eae07b commit 0fb490f

File tree

6 files changed

+356
-2
lines changed

6 files changed

+356
-2
lines changed

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

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,146 @@
6161

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

64+
先遍历左右子树,获得硬币的余额。比如,我们从左子树得到 `left = "+3"`,说明左子树有 3 个额外的硬币需要移出。如果我们从右子树得到 `right = "-1"`,说明右子树需要额外移入 1 个硬币。累加移动的次数 `abs(+3) + abs(-1)`,然后返回整个二叉树的余额 `left + right + root.val - 1`
65+
66+
返回最终的移动次数即可。
67+
6468
<!-- tabs:start -->
6569

6670
### **Python3**
6771

6872
<!-- 这里可写当前语言的特殊实现逻辑 -->
6973

7074
```python
71-
75+
# Definition for a binary tree node.
76+
# class TreeNode:
77+
# def __init__(self, val=0, left=None, right=None):
78+
# self.val = val
79+
# self.left = left
80+
# self.right = right
81+
class Solution:
82+
def distributeCoins(self, root: TreeNode) -> int:
83+
def dfs(root):
84+
nonlocal ans
85+
if root is None:
86+
return 0
87+
left, right = dfs(root.left), dfs(root.right)
88+
ans += abs(left) + abs(right)
89+
return left + right + root.val - 1
90+
91+
ans = 0
92+
dfs(root)
93+
return ans
7294
```
7395

7496
### **Java**
7597

7698
<!-- 这里可写当前语言的特殊实现逻辑 -->
7799

78100
```java
101+
/**
102+
* Definition for a binary tree node.
103+
* public class TreeNode {
104+
* int val;
105+
* TreeNode left;
106+
* TreeNode right;
107+
* TreeNode() {}
108+
* TreeNode(int val) { this.val = val; }
109+
* TreeNode(int val, TreeNode left, TreeNode right) {
110+
* this.val = val;
111+
* this.left = left;
112+
* this.right = right;
113+
* }
114+
* }
115+
*/
116+
class Solution {
117+
118+
private int ans;
119+
120+
public int distributeCoins(TreeNode root) {
121+
ans = 0;
122+
dfs(root);
123+
return ans;
124+
}
125+
126+
private int dfs(TreeNode root) {
127+
if (root == null) {
128+
return 0;
129+
}
130+
int left = dfs(root.left);
131+
int right = dfs(root.right);
132+
ans += Math.abs(left) + Math.abs(right);
133+
return left + right + root.val - 1;
134+
}
135+
}
136+
137+
```
138+
139+
### **C++**
140+
141+
```cpp
142+
/**
143+
* Definition for a binary tree node.
144+
* struct TreeNode {
145+
* int val;
146+
* TreeNode *left;
147+
* TreeNode *right;
148+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
149+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
150+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
151+
* };
152+
*/
153+
class Solution {
154+
public:
155+
int ans;
156+
157+
int distributeCoins(TreeNode* root) {
158+
ans = 0;
159+
dfs(root);
160+
return ans;
161+
}
162+
163+
int dfs(TreeNode* root) {
164+
if (!root) return 0;
165+
int left = dfs(root->left), right = dfs(root->right);
166+
ans += abs(left) + abs(right);
167+
return left + right + root->val - 1;
168+
}
169+
};
170+
```
79171
172+
### **C++**
173+
174+
```cpp
175+
/**
176+
* Definition for a binary tree node.
177+
* type TreeNode struct {
178+
* Val int
179+
* Left *TreeNode
180+
* Right *TreeNode
181+
* }
182+
*/
183+
func distributeCoins(root *TreeNode) int {
184+
ans := 0
185+
var dfs func(root *TreeNode) int
186+
dfs = func(root *TreeNode) int {
187+
if root == nil {
188+
return 0
189+
}
190+
left, right := dfs(root.Left), dfs(root.Right)
191+
ans += abs(left) + abs(right)
192+
return left + right + root.Val - 1
193+
}
194+
dfs(root)
195+
return ans
196+
}
197+
198+
func abs(x int) int {
199+
if x < 0 {
200+
return -x
201+
}
202+
return x
203+
}
80204
```
81205

82206
### **...**

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

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,133 @@
5858
### **Python3**
5959

6060
```python
61-
61+
# Definition for a binary tree node.
62+
# class TreeNode:
63+
# def __init__(self, val=0, left=None, right=None):
64+
# self.val = val
65+
# self.left = left
66+
# self.right = right
67+
class Solution:
68+
def distributeCoins(self, root: TreeNode) -> int:
69+
def dfs(root):
70+
nonlocal ans
71+
if root is None:
72+
return 0
73+
left, right = dfs(root.left), dfs(root.right)
74+
ans += abs(left) + abs(right)
75+
return left + right + root.val - 1
76+
77+
ans = 0
78+
dfs(root)
79+
return ans
6280
```
6381

6482
### **Java**
6583

6684
```java
85+
/**
86+
* Definition for a binary tree node.
87+
* public class TreeNode {
88+
* int val;
89+
* TreeNode left;
90+
* TreeNode right;
91+
* TreeNode() {}
92+
* TreeNode(int val) { this.val = val; }
93+
* TreeNode(int val, TreeNode left, TreeNode right) {
94+
* this.val = val;
95+
* this.left = left;
96+
* this.right = right;
97+
* }
98+
* }
99+
*/
100+
class Solution {
101+
102+
private int ans;
103+
104+
public int distributeCoins(TreeNode root) {
105+
ans = 0;
106+
dfs(root);
107+
return ans;
108+
}
109+
110+
private int dfs(TreeNode root) {
111+
if (root == null) {
112+
return 0;
113+
}
114+
int left = dfs(root.left);
115+
int right = dfs(root.right);
116+
ans += Math.abs(left) + Math.abs(right);
117+
return left + right + root.val - 1;
118+
}
119+
}
120+
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
/**
127+
* Definition for a binary tree node.
128+
* struct TreeNode {
129+
* int val;
130+
* TreeNode *left;
131+
* TreeNode *right;
132+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
133+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
134+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
135+
* };
136+
*/
137+
class Solution {
138+
public:
139+
int ans;
140+
141+
int distributeCoins(TreeNode* root) {
142+
ans = 0;
143+
dfs(root);
144+
return ans;
145+
}
146+
147+
int dfs(TreeNode* root) {
148+
if (!root) return 0;
149+
int left = dfs(root->left), right = dfs(root->right);
150+
ans += abs(left) + abs(right);
151+
return left + right + root->val - 1;
152+
}
153+
};
154+
```
67155
156+
### **C++**
157+
158+
```cpp
159+
/**
160+
* Definition for a binary tree node.
161+
* type TreeNode struct {
162+
* Val int
163+
* Left *TreeNode
164+
* Right *TreeNode
165+
* }
166+
*/
167+
func distributeCoins(root *TreeNode) int {
168+
ans := 0
169+
var dfs func(root *TreeNode) int
170+
dfs = func(root *TreeNode) int {
171+
if root == nil {
172+
return 0
173+
}
174+
left, right := dfs(root.Left), dfs(root.Right)
175+
ans += abs(left) + abs(right)
176+
return left + right + root.Val - 1
177+
}
178+
dfs(root)
179+
return ans
180+
}
181+
182+
func abs(x int) int {
183+
if x < 0 {
184+
return -x
185+
}
186+
return x
187+
}
68188
```
69189

70190
### **...**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int ans;
15+
16+
int distributeCoins(TreeNode* root) {
17+
ans = 0;
18+
dfs(root);
19+
return ans;
20+
}
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+
}
28+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func distributeCoins(root *TreeNode) int {
10+
ans := 0
11+
var dfs func(root *TreeNode) int
12+
dfs = func(root *TreeNode) int {
13+
if root == nil {
14+
return 0
15+
}
16+
left, right := dfs(root.Left), dfs(root.Right)
17+
ans += abs(left) + abs(right)
18+
return left + right + root.Val - 1
19+
}
20+
dfs(root)
21+
return ans
22+
}
23+
24+
func abs(x int) int {
25+
if x < 0 {
26+
return -x
27+
}
28+
return x
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
private int ans;
18+
19+
public int distributeCoins(TreeNode root) {
20+
ans = 0;
21+
dfs(root);
22+
return ans;
23+
}
24+
25+
private int dfs(TreeNode root) {
26+
if (root == null) {
27+
return 0;
28+
}
29+
int left = dfs(root.left);
30+
int right = dfs(root.right);
31+
ans += Math.abs(left) + Math.abs(right);
32+
return left + right + root.val - 1;
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def distributeCoins(self, root: TreeNode) -> int:
9+
def dfs(root):
10+
nonlocal ans
11+
if root is None:
12+
return 0
13+
left, right = dfs(root.left), dfs(root.right)
14+
ans += abs(left) + abs(right)
15+
return left + right + root.val - 1
16+
17+
ans = 0
18+
dfs(root)
19+
return ans

0 commit comments

Comments
 (0)