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. 【第十三课】监控Linux系统状态

    目录 1.查看系统负载命令:w.uptime 2.vmstat详解 3.top动态查看负载 4.sar命令(监控网卡流量) 5.nload命令(监控网卡流量) 6.iostat iotop(监控IO性 ...

  2. 【Unity Shader】从NDC(归一化的设备坐标)坐标转换到世界坐标的数学原理

    从NDC(归一化的设备坐标)坐标转换到世界坐标要点 参考资料 How to go from device coordinates back to worldspace http://feepingcr ...

  3. B1022. D进制的A+B

    除基取余法 #include<bits/stdc++.h> using namespace std; stack<int> s; int main(){ long long a ...

  4. c#版flappybird 未完全实现

    这些天开始在深圳找工作,想着把从前有些淡忘的技术再温故下.看到尊敬的<传智播客>有一期公开课,讲的是用c#编写flappybird小游戏,也就自己搜了下游戏资源,也来试试看. 其实用到的技 ...

  5. LINUX内核分析第二周学习总结——操作系统是如何工作的

    LINUX内核分析第二周学习总结——操作系统是如何工作的 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163.com/course ...

  6. SDN 交换机迁移1

    A game-theoretic approach to elastic control in software-defined networking 2014 之前的交换机迁移的工作(ElastiC ...

  7. XCODE 6.1.1 配置GLFW

    最近在学习opengl的相关知识.第一件事就是配环境(好烦躁).了解了一下os x下的OpenGL开源库,主要有几个:GLUT,freeglut,GLFW等.关于其详细的介绍可以参考opengl网站( ...

  8. mac下mongoDB的使用

    第一步: 我们在网上找到mongoDB的安装文件包,下载下来然后放在mac系统的指定位置,如图所示: 第二步:打开数据库服务端 我们在bin目录下执行mongod这个命令: 首先cd到bin目录 然后 ...

  9. [Java]Object有哪些公用方法?

    1.clone方法 保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException异常. 主要是JAVA里除了8种基本类型传 ...

  10. thinkphp5 速查表

    本速查表里的类都是think为命名空间的,实例化时省去了  use.用的时候注意. 本速查表里会有四种方法的调用: 公有方法 $class = new Class();  $class->foo ...