[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 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路径和的更多相关文章
- [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]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 ...
- 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 ...
- [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 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- 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 ...
- 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 ...
- [leetcode]Path Sum II @ Python
原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...
- [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 ...
随机推荐
- PHP创建MySQL并引入后执行sql语句
一:创建sql.php文件 <?php function sqlMethod($sql){ $servername = "localhost"; $username = &q ...
- 自定义控件,继承自 ListView
public class MyListView extends ListView { /** * 如果在xml中创建并设置了style,就会调用三个参数的. * * @param context * ...
- Element-ui学习使用
这是我使用Element-ui的布局,排布的一个界面,原本我是使用WinfowsForm来做的一个摄像头注册以及查询的小工具,目前我关注前后端的开发,所以就想着能不能把这么个小工具,我用前后端的形式开 ...
- Scala继承
override重写 为什么要用override关键字?因为这样更清楚,不容易出错,比如打错字了,就没覆盖成功,但是不会报错 override可以覆盖feild和method class Person ...
- Grok Debugger本地安装(转载)
原文链接:http://fengwan.blog.51cto.com/508652/1758845 最近在使用ELK对日志进行集中管理,因为涉及到日志的规则经常要用到http://grokdebug. ...
- 8.Mongodb备份与恢复
1.备份 语法 mongodump -h dbhost -d dbname -o dbdirectory -h:服务器地址,也可以指定端口号 -d:需要备份的数据库名称 -o:备份的数据存放位置,此目 ...
- Mysql双主操作
MySQL双主(主主)架构方案 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果 ...
- JS控制文本框输入的内容
总而言之: 先在‘<input>’ 里输入 onkeyup="value=value.replace(/[^\X]/g,'')" 然后在(/[\X]/g, ...
- Office使用技巧(不断补充)
1.word中,第一行后面有很多空格,但把第二行的退到第一行来就删了第一行的字,为什么? 解决办法:应该是下一行开头部分是一个不可拆分的整体,上一行末尾放不下,只能放在下一行.处理方法:格式--段落- ...
- python语句和语法
python语句和语法 python程序结构: 1.程序由模块构成. 2.模块包含语句. 3.语句包含表达式. 4.表达式建立并处理对象. python的语法实质上是有语句和表达式组成的.表达式处理对 ...