File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
distribute-coins-in-binary-tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -152,6 +152,8 @@ go get github.com/masx200/leetcode-test
152
152
153
153
<summary >展开查看</summary >
154
154
155
+ https://leetcode.cn/problems/distribute-coins-in-binary-tree
156
+
155
157
https://leetcode.cn/problems/binary-tree-cameras
156
158
157
159
https://leetcode.cn/problems/snail-traversal/
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments