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
}
相关推荐
TimberWill7 小时前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit7 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung7 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
天天向上102411 小时前
go 配置热更新
开发语言·后端·golang
LYFlied11 小时前
【每日算法】LeetCode 64. 最小路径和(多维动态规划)
数据结构·算法·leetcode·动态规划
Asus.Blogs12 小时前
SSE + Resty + Goroutine + Channel 完整学习笔记
笔记·学习·golang
sin_hielo12 小时前
leetcode 3074
数据结构·算法·leetcode
程序员-King.13 小时前
day124—二分查找—最小化数组中的最大值(LeetCode-2439)
算法·leetcode·二分查找
赴前尘14 小时前
golang获取一个系统中没有被占用的端口
开发语言·后端·golang
sandyznb14 小时前
go面试汇总
开发语言·面试·golang