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
}
相关推荐
科比不来it15 小时前
Go语言数据竞争Data Race 问题怎么检测?怎么解决?
开发语言·c++·golang
驰羽15 小时前
[GO]Go语言包访问控制与导入机制
golang
像风一样自由202017 小时前
Rust Tokio vs Go net/http:云原生与嵌入式生态选型指南
开发语言·golang·rust
而后笑面对17 小时前
力扣2025.10.19每日一题
算法·leetcode·职场和发展
·白小白18 小时前
力扣(LeetCode) ——11.盛水最多的容器(C++)
c++·算法·leetcode
道之极万物灭18 小时前
Go小工具合集
开发语言·后端·golang
不会写DN20 小时前
用户头像文件存储功能是如何实现的?
java·linux·后端·golang·node.js·github
玩镜的码农小师兄21 小时前
[从零开始面试算法] (04/100) LeetCode 136. 只出现一次的数字:哈希表与位运算的巅峰对决
c++·算法·leetcode·面试·位运算·hot100
earthzhang20211 天前
第3讲:Go垃圾回收机制与性能优化
开发语言·jvm·数据结构·后端·性能优化·golang
apocelipes1 天前
golang unique包和字符串内部化
java·python·性能优化·golang