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
}
相关推荐
土司大王22 分钟前
LeetCode hot100——全排列
java·算法·leetcode
203号居民44 分钟前
LeetCode hot 100 — 138. 随机链表的复制
算法·leetcode·链表
Johnston_man1 小时前
golang 安装 protoc、protoc-gen-go、protoc-gen-go-grpc、protoc-gen-grpc-gateway
后端·golang
ttwuai1 小时前
Go 后台接入单点登录后,JWT、菜单权限和接口 403 怎么验?
开发语言·后端·golang
土司大王2 小时前
LeetCode hot100——实现 Trie (前缀树)
java·算法·leetcode
麻瓜code3 小时前
【LeetCode】相交链表:双指针法,一次遍历找到交点
算法·leetcode·链表
hanlin034 小时前
刷题笔记:力扣第84题-柱状图中最大的矩形
笔记·算法·leetcode
辰烨chenye9 小时前
LeetCode Hot 100 题解 · 普通数组篇
算法·leetcode·职场和发展
辰烨chenye12 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode
码士集团小青16 小时前
从对标 Java 到对标 Go:Native AOT 的“无痛化“之路,走到哪一站了?
golang