Leetcode113. Path Sum II路径总和2
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和 sum = 22,
5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1
返回:
[ [5,4,11,2], [5,8,4,5] ]
class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > pathSum(TreeNode* root, int sum)
{
if(root == NULL)
{
return res;
}
vector<int> v;
DFS(root, v, sum);
return res;
}
void DFS(TreeNode* root, vector<int> &v, int sum)
{
if(root == NULL)
return;
if(root ->left == NULL && root ->right == NULL)
{
if(sum == root ->val)
{
v.push_back(root ->val);
res.push_back(v);
v.pop_back();
}
return;
}
v.push_back(root ->val);
if(root ->left)
DFS(root ->left, v, sum - root ->val);
if(root ->right)
DFS(root ->right, v, sum - root ->val);
v.pop_back();
}
};
Leetcode113. Path Sum II路径总和2的更多相关文章
- 113 Path Sum II 路径总和 II
给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22, 5 / \ 4 ...
- 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 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- [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 ...
- 第34-2题:LeetCode113. Path Sum II
题目 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ ...
- [Leetcode] 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路径和(返回路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode113 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 ...
随机推荐
- jquery.artDialog.source.js学习
1 关键的对象关系art = jQuery = $function artDialog() {...}artDialog.fn = artDialog.prototype = artDialog.fn ...
- 海量可视化日志分析平台之ELK搭建
ELK是什么? E=ElasticSearch ,一款基于的Lucene的分布式搜索引擎,我们熟悉的github,就是由ElastiSearch提供的搜索,据传已经有10TB+的数据量. L=LogS ...
- ajax长轮询 (转)
javaWeb项目中需要一个实时提醒的功能,也就是某人做了某一操作,可以立即提醒到当前在线的用户 最开始想在用户做了操作后,储存一个状态到数据库中然后用每隔几秒用ajax去请求后台查询数据库来确定是否 ...
- Redis理解和使用
摘抄并用于自查笔记 1. Redis简介 我们日常Java Web开发,一般使用数据库进行存储,在数据量较大的情况下,单一使用数据库保存数据的系统会因为面向磁盘,磁盘读写速度比较慢而存在严重的性能弊端 ...
- NOIP2018游记 & 退役记
NOIP2018游记 & 退役记 我是一名来自湖北武汉华中师大一附中的高二\(OIer\),学习\(OI\)一年,今年去参加\(NOIP\),然后退役.这是一篇\(NOIP2018\)的游记, ...
- 转:浅析C++中的this指针
原文出处:http://blog.csdn.net/starlee/article/details/2062586 有下面的一个简单的类: class CNullPointCall { public: ...
- input判断输入值是否合法
1.判断input输入的值是否合法有很多办法,我这里使用的是在onchange时进行判断,代码如下:[所有主要浏览器都支持] <input type="text" name= ...
- wget: command not found 解决方案
wget: command not found 解决方案 wget command not found 解决方案 问题分析 解决方案 方法一yum安装wget 方法二rpm安装 问题分析 安装的是Ce ...
- js 手机号加密 中间星号表示
var tel = String(this.memberMsg.phoneNo); var dh=tel.substr(0,3)+"******"+tel.substr(8); r ...
- Python运算符,逻辑运算
运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算,今天我们暂只学习算数运算.比较运算.逻辑运算.赋值运算 ...