leetcode 437. Path Sum III
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \ 3 -2 1 Return 3. The paths that sum to 8 are: 1. 5 -> 3 2. 5 -> 2 -> 1 3. -3 -> 11
解法1,DFS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution( object ): def pathSum( self , root, sum ): """ :type root: TreeNode :type sum: int :rtype: int # java solution public class Solution { public int pathSum(TreeNode root, int sum) { if (root == null) return 0; return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); } private int pathSumFrom(TreeNode node, int sum) { if (node == null) return 0; return (node.val == sum ? 1 : 0) + pathSumFrom(node.left, sum - node.val) + pathSumFrom(node.right, sum - node.val); } } """ def dfs(node, target): if not node: return 0 return int (node.val = = target) + dfs(node.left, target - node.val) + dfs(node.right, target - node.val) if not root: return 0 return dfs(root, sum ) + self .pathSum(root.left, sum ) + self .pathSum(root.right, sum ) |
尤其注意是dfs(root, sum)+self.pathSum(root.left, sum)+self.pathSum(root.right, sum) 而不是dfs+dfs+dfs!容易出错!
另外解法就是前缀树的解法,前缀树记录sum,比如:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \ 3 -2 1
到节点3的前缀sum就是{10:1, 15:1, 18:1},则包括节点3的满足sum=8的路径就是{10,5,3}这条通路减去{10}这条通路,两个前缀相减,更深的前缀-浅的前缀就是中间路路径,18-10=8满足条件。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution( object ): def pathSum( self , root, sum ): """ :type root: TreeNode :type sum: int :rtype: int """ def traverse(node, total, pre_sum): if not node: return 0 total + = node.val ans = pre_sum.get(total - sum , 0 ) pre_sum[total] = pre_sum.get(total, 0 ) + 1 ans + = traverse(node.left, total, pre_sum) + traverse(node.right, total, pre_sum) pre_sum[total] - = 1 return ans return traverse(root, 0 , { 0 : 1 }) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-05-25 为什么倒排索引不采用zlib这样的字典压缩算法——因为没法直接使用啊
2017-05-25 无损压缩算法历史
2017-05-25 无损压缩算法历史——熵编码是最早出现的,后来才有Lzx这些压缩算法