Leetcode 437. Path Sum III
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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- leetcode 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- LeetCode 437. Path Sum III (STL map前缀和)
找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 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 ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- [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 ...
随机推荐
- three.js 之旅 (三)
复制自:http://www.cnblogs.com/ssrsblogs/p/5611332.html 创建模型: 1.长方体: THREE.CubeGeometry(width, height, d ...
- 阿里云修改默认的ssh端口
Linux服务器的ssh服务支持远程访问服务器,默认的ssh端口号是22.为了安全起见,很多用户会将端口号由22改为其他的端口号. 如果遇到修改端口号并重启ssh服务后,新的端口号不生效,请参考以下 ...
- JBOSS只能本机localhost和127.0.0.1能访问的解决
一句话: %jboss_home%\bin>standalone.bat -Djboss.bind.address=0.0.0.0 也可以直接编辑standalone.xml,将里面所有127. ...
- Linux vmstat命令实战详解
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- 新玩具---Amazon Kindle PaperWhite 2
自从将闲置了一段时间的K3 Keyboard 3G送人后,就一直用Nexus7平板上装Kindle程序来读书,用着也挺好,没有出现很多人说的费眼问题,说来也奇怪上学毕业之后,一直从事编程相关的工作有七 ...
- 没有jquery的时候,你看看这个
vjs var br = (function() { var ua = navigator.userAgent.toLowerCase(); browser = { iPhone: /iphone/. ...
- ASP.NET Web API 安全验证之摘要(Digest)认证
在基本认证的方式中,主要的安全问题来自于用户信息的明文传输,而在摘要认证中,主要通过一些手段避免了此问题,大大增加了安全性. 1.客户端匿名的方式请求 (无认证) HTTP/ Unauthorized ...
- MATLAB 中NORM运用
格式:n=norm(A,p)功能:norm函数可计算几种不同类型的矩阵范数,根据p的不同可得到不同的范数 以下是Matlab中help norm 的解释 NORM Matrix or vector ...
- Jmeter测试工具使用
启动Jmeter: 路径:\apache-jmeter-2.13\bin\jmeter.bat 一.测试Http请求: 建立过程: 1. 测试计划--添加---Threads--线程组 2. 线程 ...
- 42-stat 显示文件的信息
显示文件的信息 stat [options] [file-list] 参数 file-list指定stat所显示的一个或多个文件的路径名 选项 -f 显示文件系 ...