mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-23 09:51:45 +08:00
40 lines
678 B
Go
40 lines
678 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
* type TreeNode struct {
|
|
* Val int
|
|
* Left *TreeNode
|
|
* Right *TreeNode
|
|
* }
|
|
*/
|
|
|
|
func distributeCoins(root *TreeNode) int {
|
|
res := 0
|
|
distributeCoinsDFS(root, &res)
|
|
return res
|
|
}
|
|
|
|
func distributeCoinsDFS(root *TreeNode, res *int) int {
|
|
if root == nil {
|
|
return 0
|
|
}
|
|
left, right := distributeCoinsDFS(root.Left, res), distributeCoinsDFS(root.Right, res)
|
|
*res += abs(left) + abs(right)
|
|
return left + right + root.Val - 1
|
|
}
|
|
|
|
func abs(a int) int {
|
|
if a > 0 {
|
|
return a
|
|
}
|
|
return -a
|
|
}
|