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
}
相关推荐
夏鹏今天学习了吗25 分钟前
【LeetCode热题100(54/100)】全排列
算法·leetcode·深度优先
钟离墨笺1 小时前
Go语言-->Goroutine 详细解释
开发语言·后端·golang
Yeats_Liao2 小时前
Go Web 编程快速入门 11 - WebSocket实时通信:实时消息推送和双向通信
前端·后端·websocket·golang
草明4 小时前
当 Go 的 channel 被 close 后读写操作会怎么样?
开发语言·后端·golang
DARLING Zero two♡6 小时前
【优选算法】D&C-Mergesort-Harmonies:分治-归并的算法之谐
java·数据结构·c++·算法·leetcode
Q741_1477 小时前
C++ 分治 归并排序 归并排序VS快速排序 力扣 912. 排序数组 题解 每日一题
c++·算法·leetcode·归并排序·分治
熬了夜的程序员17 小时前
【LeetCode】89. 格雷编码
算法·leetcode·链表·职场和发展·矩阵
Tony Bai19 小时前
【Go 网络编程全解】14 QUIC 与 HTTP/3:探索下一代互联网协议
开发语言·网络·后端·http·golang
dragoooon3419 小时前
[优选算法专题四.前缀和——NO.31~32 连续数组、矩阵区域和]
数据结构·算法·leetcode·1024程序员节
熬了夜的程序员1 天前
【LeetCode】87. 扰乱字符串
算法·leetcode·职场和发展·排序算法