问题描述:

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

思路:

仍然是深度优先遍历的原则,该方法需要多做题多巩固啊!!占了我一整天时间的一道题。

从头到尾遍历每一个节点,记录从根节点到每一个节点的sum。

通过查找当前节点和目标sum之间的差值,进行相应剪枝动作

当退出当前层次时候,需要减去当前层次计算出来的和,故每次return之前都有一个减一的动作

代码:

 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def pathSum(self, root: TreeNode, sum: int) -> int:
if root == None : return 0
prefix = {0:1}
return self.Calc(root,0,sum,prefix) def Calc(self,root,cursum,target,prefix):
if root == None : return 0
cursum += root.val
res = prefix.get(cursum - target,0)
prefix[cursum] = prefix.get(cursum,0) + 1
res += self.Calc(root.left,cursum,target,prefix) + self.Calc(root.right,cursum,target,prefix)
prefix[cursum] = prefix.get(cursum) - 1
return res

Python3解leetcode Path Sum III的更多相关文章

  1. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  2. Leetcode: Path Sum III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  3. 第34-3题:LeetCode437. Path Sum III

    题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...

  4. 47. leetcode 437. Path Sum III

    437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...

  5. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  6. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  7. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

随机推荐

  1. Celery多任务结构

    视图结构 pro_cel ├── celery_task# celery相关文件夹 │ ├── celery.py # celery连接和配置相关文件,必须叫这个名字 │ └── tasks1.py ...

  2. react 样式的写法之一 ---》styled-components的基本使用

    [react]---styled-components的基本使用---[WangQi]   一.官网地址 https://www.styled-components.com/ 二.styled-com ...

  3. Struts2异常:HTTP Status 404 - There is no Action mapped for action name addBook.

    HTTP Status 404 - There is no Action mapped for action name addBook. 在地址栏进行访问的时候,出现了这个错误信息,导致出现此异常的原 ...

  4. JavaSE编码试题强化练习5

    1.不使用函数实现字符串的翻转 /** * 1.不使用函数实现字符串的翻转 */ public class TestStringReverse { public static void main(St ...

  5. MyBatis基础面试题

    转自:http://www.cnblogs.com/huajiezh/p/6415322.html 1.Mybatis基础: #{...} 和 ${...} 的区别MyBatis将 #{…} 解释为J ...

  6. Visual Studio文件属性

    Visual Studio文件属性主要用到的为:复制到输出目录和生成操作. 复制到输出目录根据选项就可以明白是标示该文件是否输出到生成的目录地址: 生成操作则主要指该文件在执行生成时对文件执行的何种操 ...

  7. 从头到尾说一次 Java 垃圾回收,写得非常好!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:聂晓龙(花名:率鸽),阿里巴巴高级开发工程 ⬆️ 图片来源于网络 之前上学的时候有这个一个梗,说在食堂里吃饭,吃完把餐 ...

  8. Scrapy框架的应用

    一, Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有 ...

  9. WinForm的RadioButton使用小技巧

    http://www.cnblogs.com/sjrhero/articles/1883155.html 当多个RadioButton同在一个容器里面的时候,多半的操作都是要得到其中一个的值这个时候我 ...

  10. vue-fiters过滤器的使用

    1.定义过滤器 2.使用过滤器 ...... <el-table-column prop="user_gender" align="center" lab ...