技术栈

Golang | Leetcode Golang题解之第437题路径总和III

__AtYou__2024-09-29 12:43

题目:

题解:

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
}
上一篇:数据结构const char *INSTNAME[]
下一篇:详细分析Mybatis中的动态Sql(附Demo)
相关推荐
花酒锄作田
8 小时前
Go - Zerolog使用入门
golang
xlp666hub
1 天前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
xlp666hub
2 天前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
xlp666hub
2 天前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
xlp666hub
3 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
花酒锄作田
12 天前
Gin 框架中的规范响应格式设计与实现
golang·gin
琢磨先生David
13 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
超级大福宝
13 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
Charlie_lll
13 天前
力扣解题-88. 合并两个有序数组
后端·算法·leetcode
菜鸡儿齐
13 天前
leetcode-最小栈
java·算法·leetcode
热门推荐
01GitHub 镜像站点02OpenClaw 使用和管理 MCP 完全指南03本地部署 OpenClaw + DeepSeek-R1 完全指南04OpenClaw 连接飞书完整指南:插件安装、配置与踩坑记录05Window 10部署openclaw报错node.exe : npm error code 12806OpenClaw 接入 QQ Bot 完整实践指南07OpenClaw 飞书机器人不回复消息?3 小时踩坑总结08npm-error code 128问题解决方法09OpenClaw macOS 完整安装与本地模型配置教程(实战版)10OpenClaw + 飞书(Feishu)环境搭建指南