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. 原生js--跨域消息传递

    跨域消息传递:postMessage() 1.兼容性问题:IE8及其以上浏览器和其它主流浏览器都已经支持 2.使用范围:跨iframe.跨页面.跨域 3.使用方法: 发送消息:postMessage( ...

  2. SharpGL学习笔记(五) 视口变换

    视口变换主是将视景体内投影的物体显示到二维的视口平面上. 在计算机图形学中,它的定义是将经过几何变换, 投影变换和裁剪变换后的物体显示于屏幕指定区域内. 前面我们讨论过的透视投影, 正射投影, 它们都 ...

  3. Android 基于 Speex 的高度封装语音库,0 耦合,没三方jar包

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  4. Mysql [Err] 1292 - Truncated incorrect DOUBLE value: 'a'

    报错信息: [SQL] UPDATE 表 set times = 1 where type = 1 and times = 0 [Err] 1292 - Truncated incorrect DOU ...

  5. C# 队列(Queue)解决简单并发

    日志例子: private static Queue<string> m_Message = new Queue<string>(); private static bool ...

  6. mysql补充(3)优化sql语句查询常用的30种方法

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...

  7. Express 4.x Node.js的Web框架----《转载》

    本文使用node.js v0.10.28 + express 4.2.0 1 Express概述 Express 是一个简洁而灵活的node.js的MVC Web应用框架,提供一系列强大特性创建各种W ...

  8. http get请求参数拼接

    localhost:8080/hbinterface/orderInterface/groupReverseAccept.do?bizType=4&&bnetAccount=ESBTE ...

  9. Django---应用如何创建

    创建好的项目之后,需要创建各个应用模块: 创建方法: 就可以看到:index 应用

  10. ggplot2绘制概率密度图

    以下绘图以Weibull分布(韦伯分布.威布尔分布)为例 关于Weibull分布(韦伯分布.威布尔分布),请参考本人博客http://www.cnblogs.com/wwxbi/p/6141501.h ...