Leetcode 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
res = []
self.helper(root, sum, res, []) return res def helper(self, root, sum, res, path):
if not root:
return if not root.left and not root.right and root.val == sum:
path.append(root.val)
res.append([x for x in path])
return self.helper(root.left, sum - root.val, res, path + [root.val])
self.helper(root.right, sum - root.val, res, path + [root.val])
return
上面的解法由于在L22和L23中没有重新定义path,所以可以不用pop。如果重新定义path,可用如下解法。这二者的区别类似 Binary tree path 一题中 九章算法和书影的给出的解法的区别。
class Solution(object):
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
res = []
self.helper(root, sum, res, []) return res def helper(self, root, sum, res, path):
if not root:
return if not root.left and not root.right and root.val == sum:
path.append(root.val)
res.append([x for x in path])
return self.helper(root.left, sum - root.val, res, path + [root.val])
self.helper(root.right, sum - root.val, res, path + [root.val])
return
Leetcode 113. Path Sum II的更多相关文章
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [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 ...
- [LeetCode] 113. 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 ...
- leetcode 113. 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 ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- LeetCode 113. Path Sum II路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- leetcode 113 Path Sum II ----- java
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [leetcode]113. 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 ...
- Java for LeetCode 113 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 ...
随机推荐
- bzoj2286 消耗战
还是虚树的题目啊... 如果只有一个询问,我们这么考虑,可以设dp[x]为只删除x子树内和x到父亲的边,使得x这棵子树内的能源岛屿都与x的父亲不连通的最小花费. 这样如果x本身是能源岛屿,那么dp[x ...
- Python的高级特性7:闭包和装饰器
本节跟第三节关系密切,最好放在一起来看:python的高级特性3:神奇的__call__与返回函数 一.闭包:闭包不好解释,只能先看下面这个例子: In [23]: def outer(part1): ...
- iOS - 用 UIBezierPath 实现果冻效果
最近在网上看到一个很酷的下拉刷新效果(http://iostuts.io/2015/10/17/elastic-bounce-using-uibezierpath-and-pan-gesture/). ...
- c#描述异常处理语句try、catch、finally执行时的相互关系
try里面是执行代码,其中的代码"可能"产生异常. catch是对产生异常后的处理代码,可以抛出异常,也可以显示异常,也可以弹出某中提示,总之catch里是任何代码都行,如果你知道 ...
- CSS3 动画效果带来的bug
css3 动画效果比如transition:all 2s linear;这种用来计算及时的物体坐标的话会带来一定的问题 比如把一个DIV从A点移动到B点.JS为DIV.style.left=B; 但是 ...
- HTTP错误代码
服务器错误代码大全 ·400 - 错误的请求. ·401 - 访问被拒绝.IIS 定义了许多不同的 401 错误,它们指明更为具体的错误原因.这些具体的错误代码在浏览器中显示,但不在 IIS 日志中显 ...
- [TED] New video technology that reveals an objects hidden properties
通过视频中,即使1微米的震动,都能够还原成声音. 程序算法结合基础学科,能够发挥出接近无限的力量, 深入挖掘物理特性,形成你想都想不到的效果. 很多技术你都不知道,怎么和国家对抗?所以还是要遵纪守法 ...
- weblogic下部署应用时slf4j与logbak冲突的解决办法
今天在weblogic上部署一个使用logback的应用时,报错如下: java.lang.IllegalArgumentException: Invalid 'logbackConfigLocati ...
- nios II--实验4——按键中断软件部分
软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...
- BGP--边界网关协议
要全面了解BGP,首先我们要回答以下看上去很简单的问题:为什么需要BGP,也就是说BGP是如何产生的,它解决了什么问题.带着以上问题,我们先简单的回顾一个路由协议发展的轨迹. 首先路由的实质是描述一个 ...