题目

给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

给定如下二叉树,以及目标和 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的更多相关文章

  1. 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 ...

  2. Leetcode113. Path Sum II路径总和2

    给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...

  3. 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 ...

  4. 【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 ...

  5. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. python3+Appium自动化11-data数据封装之python读取csv文件

    使用背景 实际项目中,我们的测试数据可能存储在一个数据文件中,如txt.excel.csv文件类型.我们可以封装一些方法来读取文件中的数据来实现数据驱动 enumerate()简介 enumerate ...

  2. python xml dom

    http://www.cnblogs.com/coser/archive/2012/01/10/2318298.html

  3. nginx 访问路径配置

    比如: http://127.0.0.1/ 对应的物理路径 c:/a/b/c 比如:http://127.0.0.1/eec 访问的地址对应的物理路径: d:/a/b/c #user nobody; ...

  4. (转)linux常见故障一:linux 文件系统变只读

    linux常见故障一:linux 文件系统变只读 原文:https://www.cnblogs.com/ginvip/p/6375672.html 1. 重启系统看是否可以自动修复. 2. 使用fsc ...

  5. C++(SOCKET)简单爬虫制作

    先给出代码:(我使用的是VS编译器,需要在项目->project属性-> #include <iostream> #include <stdio.h> #inclu ...

  6. [一点一滴.NET]前台线程和后台线程

    前台线程和后台线程就是通过线程实例的属性IsBackground=true or false来区分的. 新建一个线程是默认是后台线程. 前台线程全部执行完之后,才退出进程. 进程退出,所有的后台线程全 ...

  7. 变量&数据类型

    php标记:四种php标记       1.<?php echo 'hello';?>       2.<? echo 'hello'; ?> //短标记       3.&l ...

  8. PHP 7 的几个新特性

    1. ?? 运算符(NULL 合并运算符) 把这个放在第一个说是因为我觉得它很有用.用法: $a = $_GET['a'] ?? 1; 它相当于: <?php $a = isset($_GET[ ...

  9. Python四大神兽(迭代器&生成器&闭包&装饰器)

    一.迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式.. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不 ...

  10. ThinkPHP find大坑 不要随便用

    举例: M("User")->find(3); $m=M("User"); $m->userName="aaa"; $m-> ...