第34-2题:LeetCode113. Path Sum II
题目
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
给定如下二叉树,以及目标和sum = 22,5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
返回:
[
[5,4,11,2],
[5,8,4,5]
]
考点
1.前序遍历
2.stack
3.递归
思路


代码
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<vector<int>> FindPath(TreeNode* root, int sum) {
// 变量
std::vector<int> path;
std::vector<vector<int>> ret;
int cur = 0;
// 入口
if(!root)
return ret;
// 出口
return FindPath(root,sum,path,cur,ret);
}
vector<vector<int>> FindPath(TreeNode* root,int sum,vector<int> &path,int cur,vector<vector<int>> &ret)
{
// 1.更新cur 和 path
cur+=root->val;
path.push_back(root->val);
// 标记叶子节点bool
bool isLeaf = !root->left && !root->right;
// 2.如果是叶子节点且路径之和等于目标值,保存path到ret中,清空path
if(isLeaf && cur == sum )
{
ret.push_back(path);
path.pop_back();
return ret;
}
// 3.如果不是叶子节点,访问其左右子树
if(root->left)
{
ret = FindPath(root->left,sum,path,cur,ret);
}
if(root->right)
{
ret = FindPath(root->right,sum,path,cur,ret);
}
// 4.递归结束之前,还原到父节点操作
cur-=root->val;
path.pop_back();
// 5.出口
return ret;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
static const void* ___ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> result;
list<int> cur;
pathSum(result, cur, root, sum);
return result;
}
private:
void pathSum(vector<vector<int>> &result, list<int> &cur, TreeNode *root, int sum) {
if (!root) return;
if (!root->left && !root->right)
{
if (sum == root->val)
{
result.emplace_back(cur.begin(), cur.end());
result.back().push_back(sum);
}
} else
{
cur.push_back(root->val);
pathSum(result, cur, root->left, sum - root->val);
pathSum(result, cur, root->right, sum - root->val);
cur.pop_back();
}
}
};
问题
1.执行用时快的原因
解析 static auto x = []() { std::ios::sync_with_stdio(false);std::cin.tie(nullptr);return 0;}()
第34-2题:LeetCode113. Path Sum II的更多相关文章
- 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 ...
- Leetcode113. Path Sum II路径总和2
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...
- LeetCode之“树”:Path Sum && Path Sum II
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...
- 【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 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 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 ...
- 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 ...
- 【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 ...
随机推荐
- 用Jmeter 测试接口--需要登录怎么办?
一.试用场景---当你测试的接口 需要登录,然后 你又不知道怎么让这测这个接口前登录?这篇文章写得是 用静态的Token 值,来测试需要登录的接口 二.步骤 1 首相用Jmeter 将要测试的接 ...
- 带你认识spark安装包的目录结构
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
- java实现连接mysql数据库单元测试查询数据项目分享
1.按照javaweb项目的要求逐步建立搭建起机构,具体的类包有:model .db.dao.test;具体的架构详见下图: 2.根据搭建的项目架构新建数据库test和数据库表t_userinfo并且 ...
- IA-32e架构下的内核初始化内存管理
初级内存管理单元 关于内存的分页 以往的物理页是按照4KB进行分配和管理的, 而在Linux之后流行的就是2MB大小的物理页的分配和管理, 整个物理内存管理单元也是2MB物理页管理的 先获取基本的物理 ...
- php数组转json对象并保留数字索引
本文要解决的问题是php不带索引的数组转换为json对象(而非默认的json数组) php中数组转json使用的方法是系统自带的:json_encode php中数组转json的规则是:当没有指定索引 ...
- C# params 动态参数
public delegate void Action(params object[] args); 再简单的东西都要强迫自己记录了,前段时间硬盘坏了,资料全没了,也没有备份,太痛苦了,那么多资料全没 ...
- xmlDemo4j解析
package lianxi; import java.util.Iterator;import java.util.List;import java.io.File;import java.io.F ...
- Spring配置文件详细分析
XML Schema命名空间作用: 1.避免命名冲突,像Java中的package一样 2.将不同作用的标签分门别类(像Spring中的tx命名空间针对事务类的标签,context命名空间针对组件的标 ...
- Spring Boot入门程序-STS
使用Eclipse EE 中的 Spring Tool插件,完成 第一个Spring Boot应用程序的创建. 一.安装Spirng Tool插件 在 Eclipse EE Oxygen版本,安装“S ...
- thinkphp的find()方法获取结果
find方法返回的是一行记录,结果是一个数组,数组的key和sql中的field相对应,假设: $res=$model->find(filed="a,b,c"); 获取结果中 ...