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. 六个创建模式之工厂方法模式(Factory Method Pattern)

    问题: 在使用简单工厂模式的时候,如果添加新的产品类,则必需修改工厂类,违反了开闭原则. 定义: 定义一个用于创建对象的接口,让子类决定具体实例化哪个产品类.此时工厂和产品都具有相同的继承结构,抽象产 ...

  2. Java中Scanner类和BufferReader类之间的区别

    java.util.Scanner类是一个简单的文本扫描类,它可以解析基本数据类型和字符串.它本质上是使用正则表达式去读取不同的数据类型. Java.io.BufferedReader类为了能够高效的 ...

  3. StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance

    http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and ...

  4. FEE Development Essentials

    FEE Development Essentials JS Basic function call() and apply() func1.bind(thisObj,arg1...argn) Cust ...

  5. R Graphics Cookbook 第3章 – Bar Graphs

    3.1 基本条形图 library(ggplot2) library(gcookbook) pg_mean   #这是用到的数据   group weight 1  ctrl  5.032 2  tr ...

  6. Android 在内部存储读写文件

    文件读写操作* Ram内存:运行内存,相当于电脑的内存* Rom内存:内部存储空间,相当于电脑的硬盘* sd卡:外部存储空间,相当于电脑的移动硬盘在内部存储空间中读写文件>小案例:用户输入账号密 ...

  7. 每日Scrum--No.4

    Yesterday:学习迪杰斯特拉算法并进行简单的编写代码 Today:继续编写代码 Problem:变量名的定义出错,造成调用的时候出错,不过改过来就好了.算法的编写不全面,漏掉个别语句,如在调试的 ...

  8. JS高级程序设计2nd部分知识要点1

    保存浮点数值需要的内存空间是保存整数值的两倍,因此ECMAScript会不失时机的将浮点数值转换为整数值 浮点数值的最高精度是17位小数 parseInt 字符串转换为数值,可传基数(8,16) pa ...

  9. GPS 坐标距离计算

    CREATE FUNCTION [dbo].[Rad]( @d float ) RETURNS float BEGIN return @d * PI()/ 180.00; End CREATE FUN ...

  10. [部署]CentOS yum源

    安装yum源 一般的软件都会提供一个.rpm的软件包,使用rpm指令安装了这个包后会自动添加一个yum仓库源,之后用yum就可以安装该软件了. 安装rpm包 rpm -ivh http://repo. ...