mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 03:11:41 +08:00
规范格式
This commit is contained in:
@ -0,0 +1,39 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user