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
}
相关推荐
阿Y加油吧3 小时前
算法实战笔记:LeetCode 169 多数元素 & 75 颜色分类
笔记·算法·leetcode
不要秃头的小孩3 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
U盘失踪了3 小时前
Go 结构体
笔记·golang
We་ct3 小时前
LeetCode 120. 三角形最小路径和:动态规划详解
前端·javascript·算法·leetcode·typescript·动态规划
py有趣4 小时前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
py有趣6 小时前
力扣热门100题之编辑距离
数据结构·算法·leetcode
水木流年追梦7 小时前
CodeTop 热门题目汇总hot300题
算法·leetcode·职场和发展
f3iiish7 小时前
2078. 两栋颜色不同且距离最远的房子 力扣
算法·leetcode
sheeta19989 小时前
LeetCode 每日一题笔记 日期:2026.04.21 题目:1722. 执行交换操作后的最小汉明距离
笔记·算法·leetcode
贺小涛9 小时前
python和golang进程、线程、协程区别
java·python·golang