package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
type TreeNode = structures.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
}