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 andsum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

[
[5,4,11,2],
[5,8,4,5]
] 题意:给定一数,在树中找出所有路径和等于该数的情况。
方法一:
使用vector向量实现stack的功能,以方便输出指定路径。思想和代码和Path sum大致相同。
/**
* 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>> res;
vector<TreeNode *> vec;
TreeNode *pre=NULL;
TreeNode *cur=root;
int temVal=; while(cur|| !vec.empty())
{
while(cur)
{
vec.push_back(cur);
temVal+=cur->val;
cur=cur->left;
}
cur=vec.back();
if(cur->left==NULL&&cur->right==NULL&&temVal==sum)
{ //和Path sum最大的区别
vector<int> temp;
for(int i=;i<vec.size();++i)
temp.push_back(vec[i]->val);
res.push_back(temp);
}
if(cur->right&&cur->right !=pre)
cur=cur->right;
else
{
vec.pop_back();
temVal-=cur->val;
pre=cur;
cur=NULL;
} }
return res;
}
};

方法二:递归法

/**
* 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>> res;
vector<int> path;
findPaths(root,sum,res,path);
return res;
}
void findPaths(TreeNode *root,int sum,vector<vector<int>> &res,vector<int> &path)
{
if(root==NULL) return;
path.push_back(root->val);
if(root->left==NULL&&root->right==NULL&&sum==root->val)
res.push_back(path); findPaths(root->left,sum-root->val,res,path);
findPaths(root->right,sum-root->val,res,path);
path.pop_back();
}
};
												

[Leetcode] Path Sum II路径和的更多相关文章

  1. [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 ...

  2. [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 ...

  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 II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  5. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

  6. LeetCode 113. Path Sum II路径总和 II (C++)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  7. leetcode 113. 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 II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  9. [leetcode]113. 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 ...

随机推荐

  1. Apache Maven(三):POM

    什么是 POM? POM (Project Object Model) 项目对象模型.它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置细节的信息.它包含大多数项目的默认值.例如,构建 ...

  2. python递归函数(计算阶乘)

    def f1(x,x1=1): if x == 1: return x1 #x1这个值为我们所需要的值,所以返回 x1 *= x r = f1(x-1,x1) #r接收返回值,并在下面接着返回 ret ...

  3. go web cookie和session

    cookie是存储在浏览器端,session是服务器端 cookie是有时间限制的,分会话cookie和持久cookie,如果不设置时间,那周期就是创建到浏览器关闭为止.这种是会话cookie,一般保 ...

  4. Linux C语言结构体-学习笔记

    Linux C语言结构体简介 前面学习了c语言的基本语法特性,本节进行更深入的学习. 预处理程序. 编译指令: 预处理, 宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 c语言表达式工具 ...

  5. go学习笔记-包处理

    包处理 package是go管理代码的重要工具,用于组织 Go 源代码,提供了更好的可重用性与可读性. 可见性 变量或函数名的首字母大写时,其就是可导出的,小写时则是不可导出的. 函数和变量的可访问性 ...

  6. P3305 [SDOI2013]费用流

    题目描述 Alice和Bob在图论课程上学习了最大流和最小费用最大流的相关知识. 最大流问题:给定一张有向图表示运输网络,一个源点S和一个汇点T,每条边都有最大流量. 一个合法的网络流方案必须满足: ...

  7. react中事件冒泡之填坑

    今天在写个组件,大致代码是这样的: class Switch extends React.Component { handlerChange = (e) => { const {onChange ...

  8. JVM内存管理机制和垃圾回收机制

    JVM内存管理机制和垃圾回收机制 JVM结构 图片描述: java源码编译成class文件 class文件通过类加载器加载到内存 其中方法区存放的是运行时的常量.静态变量.类信息等,被所有线程共享 堆 ...

  9. WCF入门三[WCF宿主]

    一.概述 WCF程序必须在宿主上运行,也就是WCF服务必须寄宿在某一个windows的进程中,可以是IIS.控制台程序.窗体程序.WAS以及所有.net程序等程序进程中.在我用VS2013创建WCF服 ...

  10. 第十五篇 Python之文件处理

    一 文件操作  介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所 ...