113. Path Sum II (Tree; DFS)
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]
]
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) {
pathGroup.clear();
if(!root) return pathGroup;
target = sum;
vector<int> path;
preOrder(root,,path);
return pathGroup;
}
void preOrder(TreeNode* node,int sum,vector<int> path){
sum = node->val + sum;
path.push_back(node->val);
if(node->left)
{
preOrder(node->left,sum,path);
}
if(node->right)
{
preOrder(node->right,sum,path);
}
if(!node->left && !node->right)
{
if(sum==target)
{
pathGroup.push_back(path);
}
}
}
private:
int target;
vector<vector<int>> pathGroup;
};
113. Path Sum II (Tree; DFS)的更多相关文章
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [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
题目链接: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
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- Path Sum II 总结DFS
https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf p ...
- [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 ...
- 113. Path Sum II
题目: 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路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
随机推荐
- js中将时间(如:2017-10-8 22:44:55)转化为时间搓,时间戳转为标准格式时间
function split_time(time){//将当前时间转换成时间搓 例如2013-09-11 12:12:12 var arr=time.split(" "); var ...
- 关于浏览器和IIS基础的简单理解
浏览器 输入域名或者IP地址,按回车访问后:发生了什么??IIS是如何工作的?为什么能这么工作?? 1 浏览器和IIS 分别是两个应用程序:浏览器访问网址实际就是 两个应用程序的数据交互往来: ...
- ngnix+uwsgi+django 部署mezzanine
以下是我用ngnix+uwsgi+django 部署mezzanine全过程,其中ngnix+uwsgi这块是看了虫师大神的博客(http://www.cnblogs.com/fnng/p/52686 ...
- centos65安装docker遇到的问题
1.安装docker后启动显示内核太低(升级内核): 网上太多方案 2.升级内核后还是启动不了docker:执行下面语句 yum install device-mapper-event-libs 步骤 ...
- 模仿std::vector写线性表的几点感想
数据结构还是很早之前学的了,当时才刚学过C语言,实现得都很简单,最近决定重新打牢基础,于是重新开始实现书上的数据结构和算法. 模仿C++ Primer的StrVec以及std::vector,使用模板 ...
- Sentinel-dashboard
Dashboard控制台 sentinel-dashboard是一个单独的应用,通过spring-boot进行启动,主要提供一个轻量级的控制台,它提供机器发现.单机资源实时监控.集群资源汇总,以及规则 ...
- Windows远程桌面连接CentOS 7
1. 安装tigervnc-server yum install tigervnc-server 2. 设置vncserver服务器 将默认提供的文件复制到/etc/systemd/system,命令 ...
- PHP处理session跨域
同一根域名下子域名之间的跨域 ini_set('session.name', 'sid'); //设置session_id的键名 ini_set('session.use_trans_sid', 0) ...
- MySQL--产品的起源和状态
MySQL这个名字,起源不是很明确.一个比较有影响的说法是,基本指南和大量的库和工具带有前缀“my”已经有10年以上,而且不管怎样,MySQL AB创始人之一的Monty Widenius的女儿也叫M ...
- cookies,sessionStorage,localStorage的区别
sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...