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
}
相关推荐
Tanecious.1 小时前
LeetCode 876. 链表的中间结点
算法·leetcode·链表
Two_brushes.9 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
凌肖战19 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
黑听人19 小时前
【力扣 简单 C】70. 爬楼梯
c语言·leetcode
ゞ 正在缓冲99%…20 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
Kaltistss21 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
程序员爱钓鱼21 小时前
Go语言项目工程化 — 常见开发工具与 CI/CD 支持
开发语言·后端·golang·gin
月忆3641 天前
Go语言的web框架--gin
golang
许愿与你永世安宁1 天前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子1 天前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展