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

本题的思路是写一个辅助函数helper(node)来return以node为起点的和为sum的path的条数。然后对所有的节点,遍历一下helper就可以了。

 class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: int
"""
if not root:
return 0
return self.helper(root, sum) + self.pathSum(root.left, sum) + self.pathSum(root.right, sum) def pathSumFromRoot(self, node, sum):
if not node:
return 0
if node.val == sum:
return 1 + self.helper(node.left, sum-node.val) + self.helper(node.right, sum-node.val)
else:
return self.helper(node.left, sum-node.val) + self.helper(node.right, sum-node.val)

Leetcode 437. Path Sum III的更多相关文章

  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 (STL map前缀和)

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

  6. 【leetcode】437. Path Sum III

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

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

  8. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  9. [LeetCode] 437. Path Sum III_ Easy tag: DFS

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

随机推荐

  1. tree命令的使用

    有些工作在linux下完成就是比在windows下完成高效! windows和linux都有tree命令,主要功能是创建文件列表,将所有文件以树的形式列出来 windows下的tree比较垃圾,只有两 ...

  2. Use Cursor

    declare : CURSOR cursor_name IS select_statement ; open : OPEN cursor_name if the query returns no r ...

  3. oracle 事务总结

    用了这么长时间的oracle,该总结一下所得了 1,事务 事务用于保证数据的一致性, 它由一组相关的 dml语句组成, 该组的dml(数据操作语言,增删改,没有查询)语句要么全部成功,要么全部失败,比 ...

  4. Router的创建者——RouteBuilder

    Router的创建者--RouteBuilder 在<注册URL模式与HttpHandler的映射关系>演示的实例中,我们总是利用一个RouteBuilder对象来为RouterMiddl ...

  5. Core Web API上使用Swagger提供API文档

    在ASP.NET Core Web API上使用Swagger提供API文档   我在开发自己的博客系统(http://daxnet.me)时,给自己的RESTful服务增加了基于Swagger的AP ...

  6. 将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete(有图有真相)(转)

    1.安装Vim和Vim基本插件首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可:lingd@ubuntu:~/arm$sudo apt-get install vim vim-scr ...

  7. android 中退出程序的两种方式

    转自:http://blog.sina.com.cn/s/blog_5da93c8f0100t76l.html 思考:如何安全的退出程序? finish是Activity的类,仅仅针对Activity ...

  8. mac系统上使用压缩包版的mysql(非安装版)

    mac本换了块固态硬盘,一切重新装过,mysql嫌官网下载太慢,直接百度 "mysql mac",第一个就是: 不料下载完后,发现这是一个压缩包版,并没有安装程序.网上搜索了一下, ...

  9. Eclipse设置和必装插件

    文章长期更新,主要是记录Eclipse好用的插件和规范的设置 插件篇: 1. StartExplorer. 在Eclipse内定位项目目录,和打开项目目录下的命令行,总是非常麻烦.有了这个插件以后,这 ...

  10. 模拟MVC-WebForm实现ModelBinding

    (一) 前言                                                                           用ASP.NET MVC的时候,我们都 ...