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]
]

 解体思路:利用递归深度优先遍历,保存中间结果。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > result;
vector<int> intermediate;
dfs(root, sum, intermediate, result);
return result;
} void dfs(TreeNode *root, int gap, vector<int> &intermediate,
vector<vector<int> > &result){
if(root == nullptr)
return;
intermediate.push_back(root->val); if(root->left == nullptr && root->right == nullptr){
if(gap == root->val)
result.push_back(intermediate);
} dfs(root->left, gap - root->val, intermediate, result);
dfs(root->right, gap - root->val, intermediate, result); intermediate.pop_back();//弹出回退
}
};
 

【leetcode】Path Sum2的更多相关文章

  1. 【leetcode】Path Sum IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  2. 【leetcode】Path Sum

    题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  3. 【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 ...

  4. 【leetcode】Path Sum I & II(middle)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径

    在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...

  6. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  7. 【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 ...

  8. 【LeetCode】Path Sum(路径总和)

    这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...

  9. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

随机推荐

  1. resource fork, Finder information, or similar detritus not allowed

    1.关闭当前项目和Xcode 2.打开终端或者iterm cd ~/Library/Developer/Xcode/DerivedData/ 3. xattr -rc . 4.重新打开项目 5.如果不 ...

  2. 如何设计一个异步Web服务——接口部分

    需求比较简单,提供一个异步Web服务供使用者调用.比如说,某应用程序需要批量地给图片加lomo效果.由于加lomo效果这个操作非常消耗CPU资源,所以我们需要把这个加lomo效果的程序逻辑放到一台单独 ...

  3. 杂谈---这些大忌,你在面试的时候发生过吗?(NO.1)

              面试是大部分人的人生当中难免会遇到的一件事,那么具体在面试当中有哪些忌讳呢? 说到面试,在这里尤其特指技术岗位的面试,很多时候,结果并不仅仅取决于你的技术广度与深度,亦或是你的笔试 ...

  4. 开发认为不是bug,你该如何处理?

    这是软件测试员面试时经常被问到的问题.看了很多答案,个人觉得作为有工作经验的测试人员回答时不能完全照搬标准答案,技术面试官想听的当然不止如此.毕竟这种情况在实际工作中也常常出现,具体问题要具体分析,你 ...

  5. Alpha阶段个人贡献分及转会人员确定

    请各个团队协商确定个人贡献分,评分根据之前个团队确定的规则进行.每个团队的个人贡献分总数为50*N,N为团队的人数. 个人贡献分要求:必须是一个自然数,每个人分数互不相同,并且和为50*N. 请各个团 ...

  6. Beta任务项录入

    今天PM把任务项整理写入TFS中,明天开始正式开发工作:

  7. Java实验报告(实验五)

    课程:Java程序设计                         班级:1351            姓名:王玮怡      学号:20135116 成绩:             指导教师: ...

  8. 第二个Spring冲刺周期团队进展报告

    第一天:找识别不了的原因 第二天:继续找识别不了的原因 第三天:找文字库,找到tessdata语言包,放到手机SD卡根目录 第四天:了解OCR引擎 第五天:将导入tess-two导入到项目中,并进行测 ...

  9. 查询过的问题关于HTML的问题

    一.Bootstrap字体图标(Glyphicons) http://www.runoob.com/bootstrap/bootstrap-glyphicons.html 二.Jquery 实现 “下 ...

  10. 使用jmeter 上传文件

    1.上传图片至阿里云时参数使用的parameters ,所以http的消息头中并没有添加content-type ,如果消息头中添加了Content-Type: multipart/form-data ...