题目:

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

Return:

[
[5,4,11,2],
[5,8,4,5]
]

分析:

和Path Sum的思路一样,链接在这里https://www.cnblogs.com/silentteller/p/10823183.html

只不过这道题需要将满足目标和的路径打印出来,我们可以传入一个空数组,每执行一次函数,便将当前的元素,也就是root->val加入到数组,当满足条件时,将数组传入进我们定义好的结果数组当中,需要注意的是,空数组需要传参,而不是传引用。

程序:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int>> res;
vector<int> s;
dfs(root, sum, s, res);
return res;
}
void dfs(TreeNode* root, int sum, vector<int> s, vector<vector<int>> &res){
if(root == nullptr) return;
s.push_back(root->val);
if(root->left == nullptr && root->right == nullptr){
if(sum == root->val)
res.push_back(s);
}
else{
dfs(root->left, sum-root->val, s, res);
dfs(root->right, sum-root->val, s, res);
}
}
};

LeetCode 113. Path Sum II路径总和 II (C++)的更多相关文章

  1. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  2. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  3. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

  4. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  5. 113 Path Sum II 路径总和 II

    给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22,              5             / \            4 ...

  6. [LeetCode] 113. 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 ...

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

  8. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

随机推荐

  1. [PHP] 再续 Laravel 5.5 接口 跨域问题 【终极暴力解决办法】

    上文中提到 Laravel5.5 使用 laravel-cors 实现 Laravel 的跨域配置 用插件来跨域 此方法能解决一部分api 请求问题 但我碰到的是 接口 请求size 超过10k,导致 ...

  2. Linux 目录详细介绍

    [常见目录说明] 目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录,是用户主目录的基点 ...

  3. Note | Ubuntu18.04安装与配置

    目标: 在服务器上配置最新的Ubuntu稳定版本18.04 LTS.18.04比16.04好看很多,非常建议. 有3块硬盘:2块4TB机械硬盘,1块2TB固态硬盘.计划将固态硬盘作为主硬盘,其余两块机 ...

  4. 不支持中国移动的N79频段,红米K30是假5G手机么?影响有多大?

    原文:https://mparticle.uc.cn/article.html?uc_param_str=frdnsnpfvecpntnwprdssskt&btifl=100&app= ...

  5. 【shell脚本】将三个数字进行升序排序===numSort.sh

    从命令输入三个数字进行升序排序(冒泡排序) 原理:比较两个相邻的元素,将值大的元素交换至右端. 脚本内容: [root@VM_0_10_centos shellScript]# cat numSort ...

  6. fastadmin表单提交后却没有关闭弹窗

    点击操作按钮弹出窗口,操作完之后提交表单,无论操作成功还是失败,窗口都不关闭,操作之后出现一个笑脸,3秒后回到弹框刚打开的样子 而我们想要的是这个效果: 在jS那里给这个按钮绑定一个事件即可实现

  7. ElasticSearch 6.7.1操作纪录

    以下操作均在 6.7.1版本中正常 c# ES客户端 测试项目地址:https://gitee.com/dhclly/IceDog.ElasticSearchClient/tree/master/sr ...

  8. linux 修改文件的时间属性

    二.修改文件时间 创建文件我们可以通过touch来创建.同样,我们也可以使用touch来修改文件时间.touch的相关参数如下: -a : 仅修改access time. -c : 仅修改时间,而不建 ...

  9. logical函数

    logical函数(逻辑函数) logical(x):x ~=0时,logical(x)=1:x = 0时,logical(x)=0

  10. MySQL for OPS 05:日志管理

    写在前面的话 日志是作为用户排查服务问题的重要依据,在 MySQL 中日志可以分为几类,各自产生着不同的作用.如 error log / bin log / slow log 等.很多时候优化数据库的 ...