题目

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

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

示例:

给定如下二叉树,以及目标和 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. BigDecimal的使用举例,包括阶乘的相加求法思路

    对于高精度要求或者运算数较大的的计算,应该使用bigdecimal类实现 import java.math.BigDecimal; public class TestSysin { public st ...

  2. C#操作Excel报错:服务器出现意外情况。

    C#操作Excel表格时,如遇以下错误: 服务器出现意外情况.(异常来自 HRESULT:0x80010105(RPC_E_SERVERFAULT)) 解决方案: 打开你电脑中的Office-Exce ...

  3. SQL Server 创建用户

    增加角色 role_for_nc 1.exec sp_addrole 'role_for_nc'; 创建一个 SQL Server 登录名wlzx,密码为"123",默认数据库为 ...

  4. Maven常用插件整理

    maven内置变量 ${basedir}表示项目根目录,即包含pom.xml文件的目录; ${version}表示项目版本; ${project.basedir}同${basedir}; ${proj ...

  5. maven课程 项目管理利器-maven 3-2 maven自动建立目录骨架

    使用cmd创建maven目录的两种方式: 使用archetype插件 1 按照提示进行选择  步骤: a 进入指定目录 b mvn archetype:generate --创建项目目录 c ente ...

  6. vue-样式问题

    问题: 今天在用vue开发单页面应用的时候,遇到一个问题,在A页面,直接刷新,页面的布局样式之类的是没有问题的,不过在B页面跳转到A页面,那么A页面有一些样式就不是预期的效果. 发现解决问题: 用调试 ...

  7. SublimeText插件eslint : 语法检测

    参考: http://www.tuicool.com/articles/faANRvj 安装之后的效果: 误用了 = ,在文件保存时就会被提示,直接顺手改掉就行了,方便的不行 步骤1:Sublime集 ...

  8. ps_cc:制作sprite拼贴图片

    我们的目标是: 将   合并为一张图片   step1:制作动作,便于批处理和重复使用 首先随便新建空白文档(建议:90x300px) 录制动作,alt+F9   上图中,可以新建分组前端,再新建动作 ...

  9. (十)JavaScript之【DOM定义】

    DOM定义Document Object Model 文档对象模型 是干什么的?改变 HTML 元素的内容 (innerHTML)改变 HTML 元素的样式 (CSS)改变 HTML 元素的属性对 H ...

  10. staticmethod classmethod property方法

    @staticmethod 静态方法 函数修饰符,用来修饰一个函数,类似于装饰器 class Dog(object): def __init__(self,name): self.name = nam ...