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 这个题目的思路就是类似于[LeetCode] 113. Path Sum II, 只不过我们不需要一定是leaf再判断, 同时recursive root.left and root.right, 最后返回总的个数即可. 1. Constraints
1) empty => 0 2. IDeas DFS T: O(n^2) worst cases, when like linked lists
T: O(nlgn) best cases, when balanced tree because height = lgn 1) edge case, if not root: return 0
2) create a helper function, get number of paths from root -> any child node s.t sum(path) == target
3) return helper(root) and recursively call root.left and root.right 3. Code
 class Solution:
def pathSum3(self, root, target):
def rootSum(root, target): # helper function to get number of paths from root -> any child node s.t sum == target
if not root: return 0
d = target - root.val
temp = 1 if d == 0 else 0
return temp + rootSum(root.left, d) + rootSum(root.right, d)
if not root: return 0
return rootSum(root, target) + self.pathSum3(root.left, target) + self.pathSum3(root.right, target)

4. Test cases

1) empty

2) 1, 1

3)

1
/ \
1 1
target = 1
4)
target = 8
      10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1

[LeetCode] 437. Path Sum III_ Easy tag: DFS的更多相关文章

  1. 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 ...

  2. [LeetCode] 437. Path Sum III 路径和 III

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

  3. LeetCode 437. Path Sum III (路径之和之三)

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

  4. leetcode 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

  5. Leetcode 437. Path Sum III

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

  6. [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  7. LeetCode 437. Path Sum III (STL map前缀和)

    找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...

  8. [LeetCode] 112. 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] 113. Path Sum II 路径和 II

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

随机推荐

  1. 流程图 --- BPMN规范简介

    BPMN 目前 是2.0规范 http://www.bpmn.org/   BPMN Quick Guide http://blog.csdn.net/flygoa/article/details/5 ...

  2. vuex报错: [vuex] Expects string as the type, but found undefined.

    报错如图 检查了好久,发现 import * as types from '../mutation-types'const actions = { add({commit}){ commit(type ...

  3. 题目1454:Piggy-Bank(完全背包问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1454 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  4. Linux操作系统定时任务系统 Cron 入门

    cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/service c ...

  5. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十三:串口模块② — 接收

    实验十三:串口模块② - 接收 我们在实验十二实现了串口发送,然而这章实验则要实现串口接收 ... 在此,笔者也会使用其它思路实现串口接收. 图13.1 模块之间的数据传输. 假设我们不考虑波特率,而 ...

  6. [工具] 各种主流 SQLServer 迁移到 MySQL 工具对比

    我之所以会写这篇对比文章,是因为公司新产品研发真实经历过这个痛苦过程(传统基于SQL Server开发的C/S产品转为MySQL云产品). 首次需要数据转换是测试环节,当时为了快速验证新研发云产品性能 ...

  7. HTML5是什么?如何鉴定HTML5产品?[转]

    转自:http://www.jscode.cn/web/v62484 Html 5开始大热标志性的事件是Apple 前CEO Steve Jobs 公开炮轰Flash,并指出Flash在移动终端的不利 ...

  8. django的queryset和objects对象

    1. queryset是查询集,就是传到服务器上的url里面的内容.Django会对查询返回的结果集QerySet进行缓存,这里是为了提高查询效率. 也就是说,在你创建一个QuerySet对象的时候, ...

  9. 9.12 Django路由

    2018-9-12 12:14:29 路由就是根据具体业务需求写的! 有的业务多,路由多,然后就需要细化 然后根据需求该! 怎么方便怎么来! 越努力,越幸运! 贴上笔记! 路由url根据需要可用修改, ...

  10. java 中的this

    this 关键字 1.在类的方法定义中使用this关键字 代表使用该方法的对象的引用 2.必须指出当前使用方法的对象是谁时 使用this 3.有时使用this可以处理方法中成员变量和参数重名的情况 4 ...