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
}
相关推荐
鹿角片ljp2 小时前
力扣140.快慢指针法求解链表倒数第K个节点
算法·leetcode·链表
im_AMBER3 小时前
Leetcode 72 数组列表中的最大距离
c++·笔记·学习·算法·leetcode
曾几何时`4 小时前
归并排序(一)
数据结构·算法·leetcode
Dream it possible!4 小时前
LeetCode 面试经典 150_图的广度优先搜索_最小基因变化(93_433_C++_中等)(广度优先搜索(BFS))
c++·leetcode·面试·广度优先
CoderYanger5 小时前
动态规划算法-两个数组的dp(含字符串数组):42.不相交的线
java·算法·leetcode·动态规划·1024程序员节
CoderYanger7 小时前
A.每日一题——1523. 在区间范围内统计奇数数目
java·数据结构·算法·leetcode·职场和发展
学学学无无止境8 小时前
力扣-从中序与后序遍历序列构造二叉树
leetcode
黑金IT9 小时前
抢占GPU ECS方案节省60-90%的成本
golang·gpu服务器
pursuit_csdn9 小时前
力扣周赛 - 479
算法·leetcode·职场和发展