path-sum-ii leetcode C++
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 andsum = 22, 5 /
4 8 / /
11 13 4 / \ /
7 2 5 1 return
[ [5,4,11,2], [5,8,4,5] ]
C++
/**
* 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>> dp;
vector<int> path;
getPath(root,sum,dp,path);
return dp;
}
void getPath(TreeNode *root, int sum,vector<vector<int>>& dp,vector<int> path){
if(NULL == root) return;
path.push_back(root->val);
if(NULL == root->left && NULL == root->right && root->val == sum )
dp.push_back(path);
getPath(root->left,sum - root->val,dp,path);
getPath(root->right,sum - root->val,dp,path);
}
};
path-sum-ii leetcode C++的更多相关文章
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- Path Sum II leetcode java
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- Path Sum II——LeetCode
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
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
随机推荐
- 集合Collection ----Set集合
set系列集合:添加的元素是 无序,不重复,无索引的 ----HashSet: 无序,不重复,无索引 ------LinkHashSet<>:有序不重复无索引(添加顺序) ----Tree ...
- Django学习day10随堂笔记
每日测验 """ 今日考题 1.默写ajax基本语法,及提交json数据和文件都需要添加哪些额外参数 2.什么是序列化,截止目前为止你所接触过的序列化有哪些 3.批量插入 ...
- PHP中的文件系统函数(三)
总算来到我们最关心的部分了,也就是 f 相关函数的操作.基本上大部分的文件操作都是以今天学习的这些内容为基础的,话不多说,我们就一个一个的来学习学习吧. 文件读取 文件的读取其实非常简单,fopen( ...
- fiddler抓包工具 https抓取 ios手机端抓取
fiddler抓包工具 https抓取 ios手机端抓取 转载链接:https://www.cnblogs.com/bais/p/9118297.html 抓取pc端https请求,ios手机端 ...
- Windows系统中的SVN使用方法
Windows 下搭建 SVN(3.9版本)服务器 2018年08月11日 12:22:55 Amarao 阅读数 11984 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议, ...
- wordpress 搭建
wordpress 搭建: 公司要使用自建地图.选用了国外的Mapplic .但是Mapplic不支持直接编辑地图,需要用json的格式定义.但是wordpress版本的就有这个鬼东西,所以搞了一个这 ...
- [转载]CentOS 7安装配置Samba服务器
假设我们有这样一个场景 共享名路径权限SHAREDOC/smb/docs所有人员包括来宾均可以访问RDDOCS/smb/tech仅允许特定组的用户进行读写访问 特定组的组名为RD,目前的Alice.J ...
- CF710F-String Set Queries【AC自动机,二进制分组】
正题 题目链接:https://www.luogu.com.cn/problem/CF710F 题目大意 \(T\)次操作 往集合中加入一个字符串 往集合中删除一个字符串 给出一个模式串求出现的集合里 ...
- P3348-[ZJOI2016]大森林【LCT】
正题 题目链接:https://www.luogu.com.cn/problem/P3348 题目大意 有\(n\)棵树开始只有一个编号为\(1\)的节点且为标记点.\(m\)次操作要求支持 在\(l ...
- mysql的一次意外
打开navcat连接本地mysql数据库的时候说mysql服务无法连接,切换到cmd用命令行来启动报错,发生系统错误5,查看百度,需用管理员权限运行, 用管理员运行依旧不好使 C:\WINDOWS\s ...