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
}
相关推荐
8***23551 天前
【Golang】——Gin 框架中间件详解:从基础到实战
中间件·golang·gin
ヽ格式化1 天前
Go与PHP变量声明全方位对比:从语法到性能的深度解析
golang·php
CoderYanger1 天前
递归、搜索与回溯-综合练习:19.目标和
java·算法·leetcode·1024程序员节
ERP老兵-冷溪虎山1 天前
Python/JS/Go/Java同步学习(第五十篇半)四语言“path路径详解“对照表: 看完这篇定位文件就通透了(附源码/截图/参数表/避坑指南)
java·javascript·python·golang·中医编程·编程四语言同步学·path路径详解
吃着火锅x唱着歌1 天前
LeetCode 3185.构成整天的下标对数目II
算法·leetcode·职场和发展
资深web全栈开发1 天前
LeetCode 1590:使数组和能被 p 整除(前缀和 + 哈希表优化)
算法·leetcode·前缀和·算法优化·哈希表·go 语言·取模运算
CoderYanger1 天前
递归、搜索与回溯-综合练习:27.黄金矿工
java·算法·leetcode·深度优先·1024程序员节
大吱佬1 天前
GO 八股整理(自用)
开发语言·后端·golang
sin_hielo1 天前
leetcode 1590
数据结构·算法·leetcode
吃着火锅x唱着歌1 天前
LeetCode 2748.美丽下标对的数目
数据结构·算法·leetcode