第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 ...
随机推荐
- PHP会话管理
Session使用 在每个页面中使用session之前,必须使用session_start() 在每个session中都可以使用$_SESSION这个全局数组,在页面必须调用session_start ...
- Kudu compaction design
不多说,直接上干货! http://blog.csdn.net/lookqlp/article/details/51438109
- 深入.NET框架。
1.Microsoft.NET框架结构是一个面向网络,支持各种用户终端的开发平台. 2..NET框架的主要内容有CLR,FCL,ADO.NET,XML,ASP.NET,WinForms和WebSeri ...
- java高级技术交流群
<明天的地平线>专注Java相关技术:SpringBoot.SpringCloud.MyBatis.Docker.微服务.集群.分布式.Linux.Jenkins.Netty.Angula ...
- 关于web.xml的welcome-file-list 配置与tomcat的关系:
关于web.xml的welcome-file-list 配置与tomcat的关系: 2018年04月18日 10:17:13 守望dfdfdf 阅读数:377 标签: welcome-file-lis ...
- #include stdio.h(6)
#include <stdio.h> int main() { //**************3.字符数组************** ] = {'i','P','\0','o','n' ...
- vue打包后CSS中引用的背景图片不显示问题
vue项目中,在css样式中引用了一张背景图片,开发环境下是可以正常显示,build之后背景图片不显示. 解决方法: 找到build/utils.js文件 修改成为如下所示内容: 添加红框中的内容即 ...
- (二)JavaScript之[函数]与[作用域]
3].函数 /** * 事件驱动函数. * 函数执行可重复使用的代码 * * 1.带参的函数 * 2.带返回值的函数 * 3.局部变量 * * 4.全局变量 * 在函数外的:不用var声明,未声明直接 ...
- Java设计模式—中介者模式
中介者模式是一种并不常用的模式,在此简单阐述阐述. 定义:用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 类型:行为类模式 ...
- Dojo 学习笔记 之 Dojo hitch&partial
原文: http://dojotoolkit.org/documentation/tutorials/1.10/hitch/index.html 版本: Dojo 1.10 为了更好地使用JavaSc ...