Golang | Leetcode Golang题解之第437题路径总和III

题目:

题解:

Go 复制代码
func pathSum(root *TreeNode, targetSum int) (ans int) {
    preSum := map[int64]int{0: 1}
    var dfs func(*TreeNode, int64)
    dfs = func(node *TreeNode, curr int64) {
        if node == nil {
            return
        }
        curr += int64(node.Val)
        ans += preSum[curr-int64(targetSum)]
        preSum[curr]++
        dfs(node.Left, curr)
        dfs(node.Right, curr)
        preSum[curr]--
        return
    }
    dfs(root, 0)
    return
}
相关推荐
哎写bug的程序员20 小时前
leetcode复盘(1)
算法·leetcode·职场和发展
雨师@1 天前
ATM 模拟器 Golang 程序--示例
开发语言·后端·golang
qq_534452521 天前
【算法 day02】LeetCode 209.长度最小的子数组 | 59.螺旋矩阵II
java·算法·leetcode·职场和发展
dying_man1 天前
LeetCode--31.下一个排列
算法·leetcode
IC 见路不走1 天前
LeetCode 第75题:颜色分类
数据结构·算法·leetcode
Navigator_Z1 天前
LeetCode //C - 757. Set Intersection Size At Least Two
c语言·算法·leetcode
你怎么知道我是队长1 天前
GO语言---匿名函数
开发语言·后端·golang
不被定义的程序猿1 天前
Golang 在 Linux 平台上的并发控制
开发语言·后端·golang
GalaxyPokemon1 天前
LeetCode - 704. 二分查找
数据结构·算法·leetcode
ifanatic1 天前
[每周一更]-(第147期):使用 Go 语言实现 JSON Web Token (JWT)
前端·golang·json