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
}
相关推荐
6Hzlia13 小时前
【Hot 100 刷题计划】 LeetCode 1143. 最长公共子序列 | C++ 二维DP 与 哨兵技巧
c++·算法·leetcode
XWalnut15 小时前
LeetCode刷题 day10
数据结构·算法·leetcode
迷藏49415 小时前
**超融合架构下的Go语言实践:从零搭建高性能容器化微服务集群**在现代云原生时代,*
java·python·云原生·架构·golang
x_xbx16 小时前
LeetCode:42. 接雨水
算法·leetcode·职场和发展
geovindu16 小时前
go: Model,Interface,DAL ,Factory,BLL using mysql
开发语言·mysql·设计模式·golang·软件构建
sheeta199816 小时前
LeetCode 每日一题笔记 日期:2026.04.14 题目:2463.最小移动距离
笔记·算法·leetcode
剑挑星河月16 小时前
55.跳跃游戏
数据结构·算法·leetcode
小辉同志17 小时前
208. 实现 Trie (前缀树)
开发语言·c++·leetcode·图论
py有趣17 小时前
力扣热门100题之括号生成
算法·leetcode
hanbr17 小时前
每日一题day2(Leetcode 704二分查找)
数据结构·算法·leetcode