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
}
相关推荐
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 118. 杨辉三角 | C++ 动态规划题解
c++·leetcode·动态规划
ywf12153 小时前
Go基础之环境搭建
开发语言·后端·golang
逆境不可逃4 小时前
LeetCode 热题 100 之 543. 二叉树的直径 102. 二叉树的层序遍历 108. 将有序数组转换为二叉搜索树 98. 验证二叉搜索树
算法·leetcode·职场和发展
副露のmagic5 小时前
哈希章节 leetcode 思路&实现
算法·leetcode·哈希算法
副露のmagic5 小时前
字符串章节 leetcode 思路&实现
windows·python·leetcode
XiYang-DING5 小时前
【LeetCode】链表 + 快慢指针找中间 | 2095. 删除链表的中间节点
算法·leetcode·链表
米粒17 小时前
力扣算法刷题 Day 29
算法·leetcode·职场和发展
wfbcg7 小时前
每日算法练习:LeetCode 125. 验证回文串 ✅
算法·leetcode·职场和发展
We་ct7 小时前
LeetCode 295. 数据流的中位数:双堆解法实战解析
开发语言·前端·数据结构·算法·leetcode·typescript·数据流
小辉同志8 小时前
739. 每日温度
c++·算法·leetcode