[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 ...
随机推荐
- javafx之HTTP协议交互
javafx端要获取获取如下信息: 服务器端获取的数据: javafx客户端发送的数据以及获取的数据: 工程目录: package Httputil; import IPsite.IPaddress; ...
- win7下KiWi Syslog服务器的安装与配置
今天就来聊聊日志服务器KiWi Syslog的安装与配置. 首先,所需文件有以下2个: 1.Kiwi_Syslog_Server_9.5.0.Eval.setup.exe[此版本只有14天寿命][Ki ...
- Visual Studio 2015 Update 1 ISO
Visual Studio Community 2015 with UPDATE 1___________________________________________English ENU - h ...
- pointer to function
指针.函数.数字.结构体.指针函数.函数指针 初学不好区分,做点儿实验来有效区分一下,以下代码采用dev-C++平台测试 //pointer to fucntion 函数功能是 基地址加偏移量得到偏移 ...
- 【转】 修改vs2010帮助文档(MSDN)路径
VS2010的MSDN采用代理网页的方式,规定首次确定目录后不能更改本地Help Library的路径,只好手动变更路径 第一步: 先把MSDN装好,先装在C盘,默认的路径 第二步 现在我要把MSDN ...
- Teleport Ultra 下载网页修复
1 三个基本正则替换 tppabs="h[^"]*"/\*tpa=h[^"]*/javascript:if\(confirm\('h[^"]*[Ult ...
- C#求任意范围内的质数
class Program { public static List<int> list; static void Main(string[] args) { Console.WriteL ...
- dyld: Library not loaded...
Libraries and frameworks are designated as Required by default, but you can change this designation ...
- phpMyAdmin上传文件大小限制
今日偶然要导一张数据表至mysql数据库中,但发现文件为2.9M,导入失败. 看一下返回的错误原因为文件超过2M的大小限制,郁闷. 找了一下“越狱”的方法,需要修改php.ini和phpmyadmin ...
- Linux学习之路—Linux文件权限
内容来源于鸟哥私房菜 1.Linux文件属性 1)第一列为文件类型与权限 第一个字符表示文件的类型: [d]表示目录 [-]表示文件 [l]表示连接文件 [b]表示设备文件中可供存储的接口设备,例如硬 ...