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. java类中元素初始化顺序详解

    父类静态变量父类静态块子类静态变量子类静态块父类普通变量父类普通块父类构造方法子类普通变量子类普通块子类构造方法

  2. Nginx反向代理 实现Web负载均衡

    实现负载均衡的方式有很多种,DNS.反向代理.LVS负载均衡器(软件实现).F5(负载均衡器,硬件,非常昂贵)这里我们只提到基于DNS,以及反向代理的方式来实现负载均衡Web服务       DNS服 ...

  3. 使用OData快速构建REST服务

    OData是微软支持的一种查询标准,它的第四版使用了REST规范,看起来简洁多了.它的最大的特点是可以在客户端自行配制查询条件,使用它构建REST服务时再也不用担心查询的扩展性问题了. 如下是几个简单 ...

  4. node升级后,项目中的node-sass报错的问题

    之前可能因为电脑不知道哪抽风了,在npm build的时候进入就卡在入口的地方,启动express的时候也会,所以就重装了一下node 重装node 其实也不是重装,就是使用 where node 查 ...

  5. Idea下Python开发平台的搭建

    1. python的下载 https://www.python.org/downloads/ 2. idea下python插件的安装 点击File->Settings...->Plugin ...

  6. 使用Redisson实现分布式锁

    原文:https://www.jianshu.com/p/cde0700f0128 1. 可重入锁(Reentrant Lock) Redisson的分布式可重入锁RLock Java对象实现了jav ...

  7. windows10使用arcgis注意事项

    平板电脑模式

  8. 【JSP EL】EL表达式里日期按照格式显示

    转:http://blog.csdn.net/kaishuaige/article/details/8505174 JSP页面用EL表达式 输出date格式     1.头上引入标签 <%@ t ...

  9. s:iterator循环输出数字

    1.在action里加上maxNum属性,GET SET方法2.第一种写法(推荐) <s:iterator value="new int[maxNum]" status=&q ...

  10. C++中经常使用到宏

    _DOS_       表示MS-DOS 16位系统平台 WIN32       表示Windows32位系统平台 WIN64       表示Windows64位系统平台 _WIN32_WCE  表 ...