[leetcode]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]
] 这个问题需要返回每条满足sum值的所有路径。思路是:后序遍历,回溯时将当前node.val添加到左右子树的结果中。 递归:
List<List<Int>> path_sum_helper(Treenode node, int current_sum, int depth, int sum_aim):
if (node is leaf-node):
r = [[]]
if (current_sum == sum_aim):
r_i = []
r_i[depth] = node.val
r.add(r_i)
return r
else:
r = [[]]
if(node.left != null):
left_ = path_sum_helper(node.left, current_sum + node.left.val, depth + 1, sum_aim)
if (left_ != null):
r = left_;
for(r_ : r):
r_[level] = node.val
// similar for right sub-tree
// need to merge left and right results
return r
[leetcode]Path Sum II的更多相关文章
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [LeetCode] 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] 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]Path Sum II @ Python
原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...
- leetcode: 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——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] 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 Path Sum II (DFS)
题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
随机推荐
- Matlab_Graphics(1)_2D
1.Add title ,axis Lables, and Legend to Graph: x=linspace(-*pi,2pi,); y1=sin(x); y2=cos(x); figure p ...
- 使用jQuery实现点击左右滑动切换特效
使用jQuery实现点击左右滑动切换特效: HTML代码如下: <!--整体背景div--> <div class="warp"> <!--中间内容d ...
- http状态消息
1-5状态码了解 1XX 表示信息(消息) 2XX 表示成功 3XX 表示重定向 4XX 表示请求错误 *** 5XX 表示服务端错误 常见状态码 200 请求成功 一切正常 301 重定向,修改后的 ...
- Java在JFinal中出现Can not create instance of class: com.keesail.web.config.WebConfig异常处理方式
编译的时候一直出现如下问题: 后面 查了许多资料 说是build项目的时候web.xml没有输出到class目录.后面试了很多方式不行.后面自己摸索出如下方式解决问题: 改成默认输出目录.
- python获取指定时间段内的随机不重复的时间点
上篇 <python时间时分秒与秒数的互相转换>http://www.cnblogs.com/gayhub/p/6154707.html 提到了把时间转成秒数的方法, 这篇写写转换成秒数后 ...
- GSM Hacking Part① :使用SDR扫描嗅探GSM网络
0×00 写在开头 近期,发现Crazy Danish Hacker在YouTuBe发布了一个挺不错的教程视频:使用SDR嗅探监听GSM网络的通信流量(GSM Sniffing Teaser – So ...
- [c++]默认参数
=================默认参数==================在函数声明时表明默认值,在函数定义时正常定义void function(int a = 2)// 函数声明void fu ...
- Pip install lxml centOSFailed building wheel for lxml
转到虚拟环境目录:yum install libxslt-devel libxml2-devel yum install python-devel pip install lxml
- 一款基于bootstrap的datetimepicker
<!DOCTYPE HTML> <html> <head> <link href="http://netdna.bootstrapcdn.com/t ...
- SQL server2000更改数据库名称
如果是SQL Server 2005可以直接右键重命名,但是SQL Server 2000中不能直接改,可以用sp_renamedb. 1.方法一(物理法): 把Old数据库改为New数据库 打开“企 ...