问题描述:

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. 一个DRF框架的小案例

    第一步:安装DRF DRF需要以下依赖: Python (2.7, 3.2, 3.3, 3.4, 3.5, 3.6) Django (1.10, 1.11, 2.0) DRF是以Django扩展应用的 ...

  2. Delphi XE2 之 FireMonkey 入门(5) - TAlphaColor

    不是 TColor, 是 TAlphaColor 了. TAlphaColor = type Cardinal; 还是一个整数. 四个字节分别是: AA RR GG BB(透明度.红.绿.蓝); 这和 ...

  3. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第1节 Scanner类_3-Scanner的使用步骤

    Scanner如何进行键盘输入,引用类型就包含了Scanner,它就是引用类型,所以也有这三个步骤, 导包.创建.使用 先通过api文档找到它.左边输入要查找scanner.双夹scanner右边就会 ...

  4. python3 configparser模块

    配置文件如下: import configparser conf = configparser.ConfigParser() print(type(conf)) #conf是类 conf.read(' ...

  5. package__init__用途

    baidu包,假设在baidu包下有N个模块,分别是baidu1.py.baidu2.py,baidu3.py, baiduHq.py(baidu1.py,baidu2.py,baidu3.py模块代 ...

  6. Codeforces 990C (模拟+组合数学)

    题面: 传送门 分析: 此题O(n2l)" role="presentation" style="position: relative;">O( ...

  7. 问题 D: 小k的硬币问题

    问题 D: 小k的硬币问题 时间限制: 1 Sec  内存限制: 128 MB提交: 21  解决: 5[提交] [状态] [命题人:jsu_admin] 题目描述 小k和小p一起玩一个游戏,有n堆硬 ...

  8. 看漫画就能学SQL,简直太cool了

    对于SQl, 很多人学不会的原因是从一开始就没明白,学这东西能干啥,学会了能有什么用.甚至有些人不知道'SQL'应该怎么读,以至于一开始兴致勃勃,但是学到一半放弃了. 注意:'sql'真的不能读成'烧 ...

  9. git把本地代码上传(更新)到github上

    # 初始化目录为本地仓库 git init # 添加所有文件到暂存去 git add . # 提交所有文件 git commit -m "init" # 添加远程仓库地址 git ...

  10. Spark Thrift Server

    ThriftServer是一个JDBC/ODBC接口,用户可以通过JDBC/ODBC连接ThriftServer来访问SparkSQL的数据.ThriftServer在启动的时候,会启动了一个Spar ...