[leetcode] 113. Path Sum II (Medium)
跟112多了一点就是保存路径
依然用dfs,多了两个vector保存路径
Runtime: 16 ms, faster than 16.09% of C++
class Solution
{
public:
vector<vector<int>> res;
vector<int> temp;
vector<vector<int>> pathSum(TreeNode *root, int sum)
{
dfs(root,temp,,sum);
return res;
}
void dfs(TreeNode *root, vector<int> cur, int total, int sum)
{
if (root == NULL)
return;
total += root->val;
cur.push_back(root->val);
if (root->left == NULL && root->right == NULL)
{
if (total == sum)
{
res.push_back(cur);
}
return;
}
if (root->left)
dfs(root->left, cur, total, sum);
if (root->right)
dfs(root->right, cur, total, sum);
}
};
[leetcode] 113. Path Sum II (Medium)的更多相关文章
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 113. 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 113. 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 113. Path Sum II路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- Leetcode 113. 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 113 Path Sum II ----- java
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [leetcode]113. 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 ...
- Java for LeetCode 113 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 ...
随机推荐
- c# json key转大小写
有需求需要将json的字段转换为小写,使用正则表达式实现,代码如下 正则表达式为 \"[a-zA-Z0-9]+\"\s*: MatchCollection ms = Regex ...
- VirtualTreeView控件
很好用的一个列表控件,可以用来代替Delphi自带的ListView和TreeView,而且也一直在更新,目前已经支持最新的XE2 官方网站:http://www.soft-gems.net SVN地 ...
- XP下安装ubuntu
一,环境说明 dell vostro 1400笔记本,winxp sp3操作系统,ubuntu-9.10-desktop-i386.iso 写这篇随笔的时候我用的已经是ubuntu了. 我是在我的移动 ...
- C++中的new,operator new与placement new
以下是C++中的new,operator new与placement new进行了详细的说明介绍,需要的朋友可以过来参考下 new operator/delete operator就是new和 ...
- Angular4初学
[1].在学习Angular4之前,首先要了解一些typescript的知识. 以下是我的总结:https://gitee.com/FangXiaoQi123/angularJSCeShi/blob/ ...
- Netty源码分析--Reactor模型(二)
这一节和我一起开始正式的去研究Netty源码.在研究之前,我想先介绍一下Reactor模型. 我先分享两篇文献,大家可以自行下载学习. 链接:https://pan.baidu.com/s/1Uty ...
- devexpress 给GridView添加行号
先找到 此时间gridView1_CustomDrawRowIndicator private void gridView1_CustomDrawRowIndicator(object sender, ...
- 系统学习 Java IO (二)----IO 异常处理
目录:系统学习 Java IO---- 目录,概览 我们使用流后,需要正确关闭 Streams 和 Readers / Writers . 这是通过调用 close() 方法完成的,看看下面这段代码: ...
- mysql产生死锁
select * from information_schema.innodb_trx; kill 对应线程ID show PROCESSLIST;
- a元素变成块状元素点击之后删除出现背景
a { text-decoration: none; background: none; -webkit-tap-highlight-color: transparent; } a:hover { - ...