leetcode 113 path Sum II 路径和

递归先序遍历+vector<int>容器记录路径
/**
* 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<int> out;
vector<vector<int>> res;
dfs(root,sum,,out,res);
return res;
}
//还是递归先序遍历+vector<int>容器记录路径
void dfs(TreeNode* node,int sum,int curSum,vector<int> &out,vector<vector<int>> &res){
if(!node) return;
curSum+=node->val;
out.push_back(node->val);
if(node->left==NULL&&node->right==NULL&&curSum==sum) res.push_back(out);
dfs(node->left,sum,curSum,out,res);
dfs(node->right,sum,curSum,out,res);
out.pop_back();
}
};
leetcode 113 path Sum II 路径和的更多相关文章
- [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路径总和 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路径和(返回路径)
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 ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 113 Path Sum II 路径总和 II
给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22, 5 / \ 4 ...
随机推荐
- java io 文件读写操作
写: import java.io.*; String filePath= "F:\\test.txt"; FileWriter fwriter = null; fwriter = ...
- CentOS 设置 yum源
什么是 yum Yum(全称 Yellow Dog Updater)是一个在 Fedora 和 RedHat 以及 CentOS 中的 Shell 前端软件包管理器.基于 RPM 包管理,能够从指定的 ...
- time、date、datetime、timestamp和year
在此声明mysql数据库 时间上总共有五中表示方法:它们分别是 time.date.datetime.timestamp和year. time : “hh:mm:ss”格式表示的时间值,格式显示TIM ...
- iptables-save - 保存 IP Tables
总览 SYNOPSIS iptables-save [-c] [-t table] 描述 DESCRIPTION iptables-save 用来将 IP Table 转储为可以简单解析的格式,输出到 ...
- SQL代码
SELECT SCHEMA_NAME(SCHEMA_ID)AS ID,name as Table_name FROM sys.tables;--查询表视图 查询表视图
- 在vue下引入jquery bootstrap
在vue 项目中引入jquery bootstrap 引入jquery npm install jquery --save-dev 在项目根目录下的build/webpack.base.conf.js ...
- web性能优化--算法优化(四)
避免for-in 把数组长度保存在局部变量中 较少迭代次数(Duffs Device) 基于函数的循环比基于循环的迭代消耗性能更多 优化if-else,一般switch比if-else速度快(hash ...
- 曝郭盛华公司30万美元收购Acn.ai域名,揭秘人工智能布局下的巨头们
据域名投资人曝料,郭盛华公司已经提前拿下.ai短域名 Acn.ai,目前域名已经设置跳转到东联科技的官网.都说域名越短越值钱,而且搜索引擎都喜欢更短的域名,例如京东更换的域名“jd.com”交易价格约 ...
- 【vue-router的基础】history了解一下
概述 window.onpopstate是popstate事件在window对象上的事件处理程序. 每当处于激活状态的历史记录条目发生变化时,popstate事件就会在对应window对象上触发. 如 ...
- 计时器 GC垃圾回收 demo
计时器: public void start() { //定义计时器 Timer timer=new Timer(); //定义运行间隔(数字越小,速度越快) int interval=30; //创 ...