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 and sum = 22,

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

return

[
[5,4,11,2],
[5,8,4,5]
]

这题是在Path Sum的基础上稍作修改。

与上题增加的动作是保存当前stack中路径的加和curSum。

当curSum等于sum时,将cur加入ret。

/**
* 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> > ret;
if(!root)
return ret; stack<TreeNode*> stk;
vector<int> cur;
cur.push_back(root->val);
int curSum = root->val;
unordered_map<TreeNode*, bool> visited;
stk.push(root);
visited[root] = true; while(!stk.empty())
{
TreeNode* top = stk.top();
if(!top->left && !top->right)
{//leaf
if(curSum == sum)
ret.push_back(cur);
} if(top->left && visited[top->left] == false)
{
stk.push(top->left);
visited[top->left] = true;
cur.push_back(top->left->val);
curSum += top->left->val;
continue;
}
if(top->right && visited[top->right] == false)
{
stk.push(top->right);
visited[top->right] = true;
cur.push_back(top->right->val);
curSum += top->right->val;
continue;
} stk.pop();
curSum -= top->val;
cur.pop_back();
}
return ret;
}
};

【LeetCode】113. Path Sum II的更多相关文章

  1. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

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

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

  3. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  6. 【LeetCode】113. 路径总和 II

    题目 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ ...

  7. 【LeetCode】666. Path Sum IV 解题报告 (C++)

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

  8. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  9. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

随机推荐

  1. hihocoder 1523:数组重排2

    题目链接 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个1-N的排列A1, A2, ... AN,每次操作小Hi可以选择一个数,把它放到数组的最左边. 请计算小 ...

  2. slf4j使用

    pom jar包引用<!-- Logging --> <dependency> <groupId>ch.qos.logback</groupId> &l ...

  3. li 标签中放a 标签,文字垂直居中

    <ul style="float:left"><li class="lili"> <a href="Left.aspx? ...

  4. Java容器-引用数据类型排序+TreeSet、TreeMap底层实现

    目录 1.冒泡排序的实现 2.比较接口(普通数据类型.引用数据类型) 普通数据类型:冒泡排序 引用数据类型:包装类(Integer.String.Character.Date) 自定义类型:实体类:i ...

  5. linux查看端口被哪个服务占用的命令

    netstat -tunpl | grep 6379

  6. 使用OllyDbg破解软件

    好,废话不多说,教程开始.  我们首先查壳,是Aspark的壳,对于这个壳,大家应该很熟了.<ignore_js_op>   我已经脱好了壳,再查一下壳,是Dephi的<ignore ...

  7. poj 3258 River Hopscotch 题解

    [题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...

  8. go语言基础之不定参数的传递

    1.不定参数的传递 示例1: package main //必须有一个main包 import "fmt" func myfunc(tmp ...int) { for _, dat ...

  9. RecyclerView.ItemDecoration 间隔线

    内容已更新到:https://www.cnblogs.com/baiqiantao/p/19762fb101659e8f4c1cea53e7acb446.html 目录一个通用分割线ItemDecor ...

  10. 详细记录ASP.NET中的图象处理

    最近做网站时,要求上传能加上水印,就研究了一下相关的功能.推荐一下程序人生的网站,大家也可以写一些开发感悟在上面.在使用ASP的时候,我们时常要借助第三方控件来实现一些图象功能.而现在,ASP.NET ...