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. 搭建django虚拟环境完整步骤

    一.建立虚拟环境 pip install virtualenv 要使用Django,首先要建立一个虚拟工作环境.我们先为项目建立一个文件夹learn,在文件夹中打开命令行(shift+右击),来建立另 ...

  2. 浅谈iOS 自动调节文本高度

    文字展示是任何GUI开发的一个最常规的编程任务.可能一提及文字我们脑中想到的无非就是 Label 和 Text 这两个关键词,今天我们就谈谈 Label. 无论在 Windows 或者 Web 开发中 ...

  3. javascript source map 的使用

    之前发现VS.NET会为压缩的js文添加一个与文件名同名的.map文件,一直没有搞懂他是用来做什么的,直接删除掉运行时浏览器又会报错,后来google了一直才真正搞懂了这个小小的map文件背后的巨大意 ...

  4. proe工程图输出dwg/dxf文件设置

    网上看到不少人分享proe转转dxf/dwg配置文件的,但是看了一圈,几乎都没有涉及到转化线型的,所以自己整理自己的配置文件,写在这里分享出来. 以Pro/engineer WF5.0为例: 1.复制 ...

  5. 了不起的Node.js--之一

    在OSX下安装Nodejs 从Node.js官网下载PKG文件,其文件名格式遵循node-v.?.?.?.pkg.若要通过手动编译来进行安装,请确保机器上已安装了XCode,然后根据Linux下的编译 ...

  6. Java serialVersionUID作用和生成

    序列化和反序列化Java是面向对象的语言,与其他语言进行交互(比如与前端js进行http通信),需要把对象转化成一种通用的格式比如json(前端显然不认识Java对象),从对象到json字符串的转换, ...

  7. Top 10 Javascript MVC 框架

    在网上偶然看到了,几种MVC框架各有优缺点,但Backbone和Ember的呼声相对更高-大家参考一下哈- http://codebrief.com/2012/01/the-top-10-javasc ...

  8. 【SE】Week1 : 四则运算题目生成器批改器程序总结

    用户需求详见:http://www.cnblogs.com/jiel/p/4810756.html 1)PSP表格分析(预计耗时): PSP2.1 Personal Software Process ...

  9. Daily Scrum NO.3

    工作概况 符美潇(PM) 昨日完成的工作 1.Daily Scrum.日常会议及日常工作的分配和查收. 2.整合各DEV所写的代码,在TFS上进行Beta阶段第一次代码签入. 今日工作 1.Daily ...

  10. week9:个人博客作业

    团队作业(5) 以下内容多数是网上的内容,只是做了整合的过程. 要求 在PM 带领下, 每个团队深入分析下面行业的App, 找到行业的Top 5 (从下面的三个备选中,任选一个行业即可) 英语学习/词 ...