LeetCode OJ: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 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]
]
求路径的和与某一特定的值时候对等即可,简单的dfs,代码如下:
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<int> res;
dfs(root, res, sum);
return ret;
}
void dfs(TreeNode * root, vector<int> res, int left)
{
if(!root) return;
if(!root->left && !root->right && left == root->val){
res.push_back(root->val);
ret.push_back(res);
}
if(left <= root->val)
return;
else{
res.push_back(root->val);
if(root->left)
dfs(root->left, res, left -= root->val);
if(root->right)
dfs(root->right, res, left -= root->val);
}
}
private:
vector<vector<int>> ret;
};
java版本代码如下,方法相同,就是java的引用处理起来稍微麻烦点,递归尾部应该pop一下。
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
Stack<Integer> tmp = new Stack<Integer>();
dfs(ret, tmp, root, sum);
return ret;
}
public void dfs(List<List<Integer>> ret, Stack<Integer> tmp, TreeNode root, int remain){
if(root == null) return;
tmp.push(root.val);
if(root.left == null && root.right == null)
if(remain == root.val)
ret.add((Stack<Integer>)tmp.clone());
if(root.left != null) dfs(ret, tmp, root.left, remain - root.val);
if(root.right != null) dfs(ret, tmp, root.right, remain - root.val);
tmp.pop();
}
}
LeetCode OJ:Path Sum II(路径和II)的更多相关文章
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- leetcode 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- [LeetCode] Minimum Path Sum 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- leetcode:Path Sum (路径之和) 【面试算法题】
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- LeetCode 112. Path Sum(路径和是否可为sum)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [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] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
随机推荐
- 0402-服务注册与发现-Eureka Server使用、将服务注册到Eureka server上
一.Eureka Server使用 官方文档地址:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud. ...
- Java基础—复用类
复用代码是Java众多引人注目的功能之一. 一般而言,实现代码重用java提供了两种方式:组合以及继承. 组合:新的类由现有类的对象所组成.(复用现有代码的功能,而非它的形式) 继承:按照现有类的类型 ...
- 20170401 错了两天的-XML解析
你不找到的话,错误就在那里.你找到了错误才会成为财富! Strans XML 解析3要素:1.源xml 格式正常, eg. '<?xml version="1.0" enco ...
- LeetCode:整数转罗马数字【12】
LeetCode:整数转罗马数字[12] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...
- Oracle索引(2)索引的修改与维护
修改索引 利用alter index语句可以完成的操作 重建或合并索引 回收索引未使用的空间或为索引非配新空间 修改索引是否可以并行操作及并行度 修改索引的存储参数以及物理属性 指定Logging ...
- Shell 语句
一 test 测试: 测试命令 test [ ] [[ ]] (( ))打开man test 逐一介绍每个参数 浮点计算:echo 'scale=2;1/3'|bc -l 测试操作命令执行后会返回到 ...
- crm销售管理系统一
一.环境配置 1.首先配置pip,环境变量配置 pip 9.0.1 from c:\users\administrator\envs\crm\lib\site-packages (python 3.6 ...
- Cocos2d-x项目移植到WP8系列之四:文件操作
原文链接: http://www.cnblogs.com/zouzf/p/3972457.html 读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建.删除.判 ...
- Eclipse4.2安装样式插件
1.插件地址 http://eclipse-color-theme.github.com/update 点击Eclipse菜单 Help>>Install New Software... ...
- Linux自定义别名alias重启失效问题
Linux上的别名功能非常方便,例如ll可以显示文件列表的长信息,但是却不是以human能读懂的方式显示,所以我尝试直接在命令行中自定义一个别名: alisa lk='ls -lh' 然后lk就能正常 ...