Skip to content

Commit da62e8f

Browse files
author
shixuan.ma
committed
https://leetcode.cn/problems/distribute-coins-in-binary-tree
1 parent 6ef1797 commit da62e8f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ go get github.com/masx200/leetcode-test
152152

153153
<summary>展开查看</summary>
154154

155+
https://leetcode.cn/problems/distribute-coins-in-binary-tree
156+
155157
https://leetcode.cn/problems/binary-tree-cameras
156158

157159
https://leetcode.cn/problems/snail-traversal/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
function distributeCoins(root: TreeNode | null): number {
4+
let ans = 0;
5+
function dfs(node: TreeNode | null): number {
6+
if (node === null) {
7+
return 0;
8+
}
9+
const d = dfs(node.left) + dfs(node.right) + node.val - 1;
10+
ans += Math.abs(d);
11+
return d;
12+
}
13+
dfs(root);
14+
return ans;
15+
}
16+
export default distributeCoins;

0 commit comments

Comments
 (0)