[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 ...
随机推荐
- Apple Pay的快速实现
一.在Apple开发者中心配置 AppleID 和 Merchant IDs 二.配置好证书后在Xcode中开启Apple Pay 三.代码实现 3.1 判断是否支持Apple Pay,如果支持又将支 ...
- [Python]记录详细调用堆栈的日志
参考http://www.cnblogs.com/tuzkee/p/3243110.html import sys import os def detailtrace(info): retStr = ...
- Android深度探索--HAL与驱动开发----第六章读书笔记
Linux驱动程序与其他类型的Linux程序一样拥有自己的规则,下面给出一个编写基本的Linux驱动的一般步骤: (1)建立Linux驱动的骨架(装载和卸载Linux驱动): (2)注册和注销设备文件 ...
- 如何利用Matlab进行ROC分析
ROC曲线基本知识: 判断分类器的工作效率需要使用召回率和准确率两个变量. 召回率:Recall,又称"查全率", 准确率:Precision,又称"精度".& ...
- C语言复杂声明-void (*signal(int sig, void (*handler)(int)))(int);
问题提出 请分析此声明:void (*signal(int sig, void (*handler)(int)))(int); 求解过程 在对上面的例子作分析之前,我们需要了解C语言的声明优先级,&l ...
- zynq中uboot的qspi启动报错及解决办法
问题描述: 用u-boot-xlnx-v2016.3版本编译的uboot通过qspi flash启动出现如下错误: 尝试在uboot命令行输入"sf probe 0 0 0"挂载q ...
- 后缀名“.dll .obj .lib”和“ .so .o .a”文件的区别含义
(1) .dll .obj .lib使用在windows平台下. .dll:动态链接库,作为共享函数库的可执行文件. .obj:目标文件,相当于源代码对应的二进制文件,未经 ...
- 第三次作业 GIThub操作
一.Git 客户端操作 1.在project文件夹初始化一个repository 2.添加并提交readme.txt 3.修改并查看状态 4.多次修改并提交 5.创建分支mv1并checkout至该分 ...
- 浏览器 私有属性&内核
-moz代表firefox浏览器私有属性 -ms代表IE浏览器私有属性 -webkit代表chrome.safari私有属性 IE使用的是Trident内核,Firefox 使用的是Gecko内核.目 ...
- iOS学习之iOS沙盒(sandbox)机制和文件操作(二)
1.获取程序的Home目录 NSString *homeDirectory = NSHomeDirectory(); NSLog(@"path:%@", homeDirectory ...