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
}
相关推荐
小南家的青蛙2 小时前
LeetCode第1261题 - 在受污染的二叉树中查找元素
算法·leetcode·职场和发展
玖剹2 小时前
记忆化搜索题目(二)
c语言·c++·算法·leetcode·深度优先·剪枝·深度优先遍历
Jul1en_3 小时前
【算法】分治-归并类题目
java·算法·leetcode·排序算法
java修仙传3 小时前
力扣hot100:寻找旋转排序数组中的最小值
算法·leetcode·职场和发展
F_D_Z6 小时前
哈希表解Two Sum问题
python·算法·leetcode·哈希表
LYFlied6 小时前
【每日算法】LeetCode124. 二叉树中的最大路径和
数据结构·算法·leetcode·面试·职场和发展
小妖6667 小时前
力扣(LeetCode)- 93. 复原 IP 地址(JavaScript)
javascript·tcp/ip·leetcode
前端小白在前进9 小时前
力扣刷题:复原IP地址
tcp/ip·算法·leetcode
yaoh.wang9 小时前
力扣(LeetCode) 94: 二叉树的中序遍历 - 解法思路
python·算法·leetcode·面试·职场和发展·二叉树·跳槽
资深web全栈开发9 小时前
并查集(Union-Find)套路详解
leetcode·golang·并查集·unionfind