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
}
相关推荐
Aaswk2 小时前
刷题笔记(回溯算法)
数据结构·c++·笔记·算法·leetcode·深度优先·剪枝
Frostnova丶2 小时前
(11)LeetCode 239. 滑动窗口最大值
数据结构·算法·leetcode
参.商.3 小时前
【Day48】46. 全排列
leetcode·golang
liuyao_xianhui6 小时前
优选算法_栈_删除字符中的所有相邻重复项_C++
开发语言·数据结构·c++·python·算法·leetcode·链表
老虎06276 小时前
LeetCode热题100 刷题笔记(第三天)链表 「两数相加」
笔记·leetcode·链表
会编程的土豆6 小时前
【leetcode hot 100】二叉树
算法·leetcode
Mr_Xuhhh7 小时前
LeetCode 热题 100 刷题笔记:数组与排列的经典解法(续)
算法·leetcode·职场和发展
Mr_Xuhhh8 小时前
LeetCode 热题 100 刷题笔记:高频面试题详解(215 & 347)
算法·leetcode·排序算法
童话ing8 小时前
【LeetCode】239.滑动窗口最大值
数据结构·算法·leetcode·golang
不会写DN8 小时前
Go 中最主流 JWT 库 jwt -go
开发语言·后端·golang