题目

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

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

示例:

给定如下二叉树,以及目标和 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. 性能测试工具LoadRunner15-LR之负载生成器(Load Generators)

    简介 对场景进行设计后,需要对负载生成器进行管理和配置.Load Generators是运行脚本的负载引擎(相当于加压机)主要功能是生成虚拟用户进行负载,在默认情况下使用本地的负载生成器来运行脚本. ...

  2. Golang笔记(二)面向对象的设计

    Golang笔记(二)面向对象的设计 Golang本质还是面向过程的语言,但它实现了一些OOP的特性,包括抽象.封装.继承和多态. 抽象和封装 Golang和C语言一样以struct为数据结构核心,不 ...

  3. Unable to run man pages on Centos 6

    I just installed CentOS 6 with minimal install. When i tried to read the linux manual pages using ma ...

  4. EntityFramework报错

    解决:未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” 使用 ...

  5. iOS 上滑隐藏导航,下滑显示导航,仿斗鱼导航效果

    UItableView或 UIcollectionView 都是继承UIScrollView 滑动的时候,判断是上滑还是下滑 使用 UIScrollView 的代理方法 func scrollView ...

  6. Java基础入门 - 标识符及其命名规范

    类名.变量名.方法名都称为标识符 标识符命名规范: 由字母(A-Z或a-z).数字.下划线(_)和美元符($)中的一种或多种组合而成 不可以数字开头 大小写敏感 关键字不能用作标识符 合法标识符如:D ...

  7. apache安装 windows

    进入cmd cd apache目录 httppd.exe ?显示全部命令 httppd.exe -k install 安装apache httppd.exe -k start 启动 检测是否运行 浏览 ...

  8. eclipse svn 环境搭建

    subversion 1.8.14(r1692801) eclipse 安装插件 1.10.x版本

  9. 【工作中学习2】Map的使用及排序(第三个参数)

    项目进行中,使用到Map(std::map),Map要点整理如下: 1. Map,也叫关联数组,提供key/value(键/值对),key用来索引,value是被存储和检索的数据. 2. key值唯一 ...

  10. u-boot分析(二)----工作流程分析

    u-boot分析(二) 由于这两天家里有点事,所以耽误了点时间,没有按时更新,今天我首先要跟大家说说我对于u-boot分析的整体的思路,然后呢我以后的博客会按照这个内容更新,希望大家关注. 言归正传, ...