Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit b9eb2d6

Browse files
committed
979 finish
1 parent fafc688 commit b9eb2d6

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed
Loading
Loading
Loading
Loading

Algorithms/0979.distribute-coins-in-binary-tree/distribute-coins-in-binary-tree.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ import "github.com/aQuaYi/LeetCode-in-Go/kit"
66
type TreeNode = kit.TreeNode
77

88
func distributeCoins(root *TreeNode) int {
9+
res := 0
10+
dfs(root, &res)
11+
return res
12+
}
13+
14+
func dfs(node *TreeNode, res *int) int {
15+
if node == nil {
16+
return 0
17+
}
18+
l, r := dfs(node.Left, res), dfs(node.Right, res)
19+
*res += abs(l) + abs(r)
20+
return l + r + node.Val - 1
21+
}
922

10-
return 0
23+
func abs(a int) int {
24+
if a < 0 {
25+
return -a
26+
}
27+
return a
1128
}

0 commit comments

Comments
 (0)