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 ...
随机推荐
- 51nod 2589 快速讨伐
51nod 如果不考虑升级操作,只有买装备操作和打怪操作,那么首先一定要先买装备,然后可以打死1级的怪,这些怪被打死的时间只要在第一次买装备后面好了,因为现在总操作是\(n+\sum a_i\)个,所 ...
- 关于mysql varchar(N)
varchar(N) 能存多少个中文字符? 4.0版本以下,varchar(50),指的是50字节,如果存放UTF8汉字时,只能存16个(每个中文3字节) gbk :每个字符最多占用2个字节 utf8 ...
- 启动angular项目,端口被占用
启动项目,npm start,报如下错误: 解决:1.win+R,输入CMD,启动命令行窗口. 2.输入netstat -ano或者netstat -ano|findstr 8080,查看8080端口 ...
- curry&unCurry函数
unCurry函数与curry函数区别:curry化函数:固定部分参数,返回一个接受剩余参数的新函数,目的是为了缩小适用范围,创建一个针对性更强的函数. unCurry化函数:扩大适用范围,创建一个应 ...
- Spring Boot嵌入式的Servlet容器
一.查看SpringBoot默认的嵌入式Servlet容器(默认使用的是tomcat) 在IDEA的项目的pom文件中按Ctrl + shift + Alt + U可以打开SpringBoot依赖的图 ...
- 软件开发中oracle查询常用方法总结
上次新霸哥和大家讲解了一些关于oracle的知识发现大家对oracle还是比较感兴趣的,下面新霸哥就大家比较关系的oracle中常用的查询有哪几种?做个和oracle相关的开发的朋友可能会知道答案,但 ...
- Spring基础20——AOP基础
1.什么是AOP AOP(Aspect-Oriented Programming)即面向切面编程,是一种新的方法论,是对那个传统OOP面向对象编程的补充.AOP的主要编程对象是切面(aspect),而 ...
- 爬虫获取网页数据,报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start by
https://blog.csdn.net/hj_xy_0705/article/details/85011072
- IBM小机拆镜像换盘
1.硬盘告警信息 2.故障排查 查看错误日志 # errpt -aj C62E1EB7 查看hdisk0的信息,发现hdisk0属于rootvg # lspv 查看hdi ...
- java常用类与包装类--包装类
2.基本数据类型数据的包装类 局部变量中基本数据类型直接分配在栈中,而对象分配在堆中 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法来操作该数据 包装类主要功能:用于基本数据类型与字 ...