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 迭代法的更多相关文章

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

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

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

  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]Path Sum II @ Python

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

  6. 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 ...

  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 II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  9. 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 ...

随机推荐

  1. SSM框架整合总结

    关于ssm整合的相关总结: 1.持久层--->mybatis:通过Spring 来管理持久层的 Mapper (相当于 dao 接口),来完成对数据库的操作. 首先我们回顾一下,在单独使用myb ...

  2. Android 手机卫士12--进程管理

    1.本进程不能被选中,所以先将checkbox隐藏掉--手机卫士 不能自杀 if(getItem(position).packageName.equals(getPackageName())){ ho ...

  3. 如何给span设置高度宽度?

    内容提要:给Span设置高度和宽度后没有作用.本文介绍了如何如何给span设置高度宽度. CSS模型中经常用的容器是DIV和span. 给Span设置高度和宽度后没有作用. <style typ ...

  4. 【GOF23设计模式】外观模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_外观模式.公司注册流程.迪米特法则 package com.test.facade; public interface 工 ...

  5. ADODB.Connection 错误 ‘800a0e7a’ 未找到提供程序

    问题表现:做网站ASP页面提示:ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. 解决方案:一般都是64位系统的原因,把IIS切换为32Bit模式运 ...

  6. 破解入门【OllyDebug爆破程序】

    逆向破解这块我也是个刚起步的小菜,入门都还算不上吧,看了点基础教程,先动手练习一下增加点兴趣.嘿嘿 工具: peid         //查壳工具 OllyDebug    //反汇编.动态调试工具 ...

  7. 高性能JS笔记2——数据存取

    数据存取性能而言: 字面量>本地变量>数组元素>对象成员 一.标识符解析的性能 标识符解析是有代价的,一个标识符的位置越深,它的读写速度也就越慢. 局部变量的读写速度是最快的,全局变 ...

  8. Android WelcomeActivity 启动画更换网络图片

    1.运行效果  第一张是本地的启动图,第二张是网络启动图       2.用到的第三方jar包   Android-Universal-Image-Loader-master 不熟的请看  Andro ...

  9. iOS-多线程--介绍NSThread和GCD及其它们的线程通讯示例

    前言:下面就不一一列出 pthread.NSThread.GCD.NSOperation 的完整的各种方法了,只分别将最常用的列出来,以便偶尔瞄一眼. 一.NSThread 1> 线程间的通讯/ ...

  10. Android网络编程只局域网传输文件

    Android网络编程之局域网传输文件: 首先创建一个socket管理类,该类是传输文件的核心类,主要用来发送文件和接收文件 具体代码如下: package com.jiao.filesend; im ...