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
}
相关推荐
宁瑶琴11 小时前
COBOL语言的云计算
开发语言·后端·golang
倦王13 小时前
力扣日刷47-补
python·算法·leetcode
无限进步_14 小时前
【C++】巧用静态变量与构造函数:一种非常规的求和实现
开发语言·c++·git·算法·leetcode·github·visual studio
凌波粒15 小时前
LeetCode--344.反转字符串(字符串/双指针法)
算法·leetcode·职场和发展
啊哦呃咦唔鱼15 小时前
LeetCode hot100-543 二叉树的直径
算法·leetcode·职场和发展
样例过了就是过了16 小时前
LeetCode热题100 爬楼梯
c++·算法·leetcode·动态规划
ThisIsMirror16 小时前
leetcode 452 Arrays.sort()排序整数溢出、Integer.compare(a[1], b[1])成功的问题
算法·leetcode
x_xbx17 小时前
LeetCode:438. 找到字符串中所有字母异位词
算法·leetcode·职场和发展
m0_6948455717 小时前
UVdesk部署教程:企业级帮助台系统实践
服务器·开发语言·后端·golang·github
@atweiwei18 小时前
Go语言面试篇数据结构底层原理精讲(下)
数据结构·面试·golang