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.
通过一个p指针,遍历 二叉树,并将每次的值 保存在 sum2 中 。
遇到右节点,将右节点+depth 保存在 temp中,当再次使用 该节点时,根据depth 将sum2中的长度削减成 depth-1
/**
* Definition for a binary tree node.
* 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>> re;
if(root==NULL) return re;
TreeNode *p=root; int depth=;
vector<pair<TreeNode * ,int>> temp; // 保存右节点+depth
vector<int> sum2; // 保存一条路径的所有点值
pair<TreeNode *, int > t=make_pair(root,depth);
// temp.push_back(t);
while(!temp.empty()||p!=NULL){
sum2.push_back(p->val);
if(p->left!=NULL){
if(p->right!=NULL){
temp.push_back(make_pair(p->right,depth+));
}
p=p->left;
depth++;
}
else{
if(p->right==NULL){
int result=;
for(int i=;i<sum2.size();i++)
{
result=result+sum2[i];
}
if(result==sum)
re.push_back(sum2);
if(temp.empty()) break;
p=(*(temp.end()-)).first;
depth=(*(temp.end()-)).second;
temp.erase(temp.end()-);
sum2.erase(sum2.begin()+depth-,sum2.end());
}
else{
p=p->right;
depth++;
}
}
}
return re;
}
};
leetcode: Path Sum II 迭代法的更多相关文章
- [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] 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——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 深度搜索
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 (DFS)
题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
随机推荐
- 二、SQL语句映射文件(2)增删改查、参数、缓存
//备注:该博客引自:http://limingnihao.iteye.com/blog/106076 2.2 select 一个select 元素非常简单.例如: Xml代码 收藏代码 <!- ...
- ahjesus 获取当前方法被调用执行的具体位置,包括命名空间和方法
MethodBase method = ).GetMethod(); string ahjesus = method.ReflectedType.FullName + "." + ...
- javascript宿主对象之window.history
window.historys属性允许我们操作同一个浏览器回话中的已访问页面,例如我们可以看到在这之前我们浏览页面的数量: window.history.length 由于隐私保护,我们无法获取这些页 ...
- MYSQL使用正则表达式过滤数据
一.正则与LIKE的区别 Mysql的正则表达式仅仅使SQL语言的一个子集,可以匹配基本的字符.字符串.例如:select * from wp_posts where post_name REGEXP ...
- English Training Material - 05
Could I leave a message? Language Checklist Telephoning (1) Introducing yourself Good morning, Arist ...
- JavaWeb开发必过关-Servlet学习(一)
一.什么是Servlet servlet其实是一个小程序,它是运行在服务器上的,一个servlet就是一个Java类,可以通过"请求-响应"编程模型来访问这个驻留在服务器内存的Se ...
- 使用SharedPreferences进行简单的储存
博客地址 http://www.cnblogs.com/mmyblogs/p/6082512.html(转载请保留) SharedPreferences定义 1.是一种轻型的数据存储的方式 2.本质是 ...
- 错误:找不到类org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
- 使用greenDAO生成DAO代码
研究greenDAO有几天了,刚开始看别人的博客基本都把我带到了沟里,讲gradle把简单的问题搞得非常复杂,而且都是抄来抄去,看来看去也就那么几篇,实在看不下去了,只得硬着头皮自己琢磨,好在终于把这 ...
- iOS-多线程之NSThread详解
前言 线程是用来执行任务的,线程彻底执行完任务A才能去执行任务B.为了同时执行两个任务,产生了多线程. 我打开一个视频软件,我开辟一个线程A让它执行下载任务,我开辟一个线程B,用来播放视频.我开辟两个 ...