LeetCode 113. Path Sum II路径总和 II (C++)
题目:
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++)的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- 113 Path Sum II 路径总和 II
给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22, 5 / \ 4 ...
- [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 ...
- 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 ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
随机推荐
- VScode Python 虚拟环境
安装python环境 在VScode里设置Python 虚拟环境查找路径(Settings -> Extensions -> Python Configurations -> Env ...
- git--配置文件、.gitignore
配置文件 git给我们提供了三种配置文件的方法,一种是项目配置文件,一种是全局配置文件,还有一种是系统配置文件. 在我们第一次使用git commit提交代码的时候,git让我们配置用户名和邮箱 全局 ...
- hdu6514 一维化 + 二维前缀和
http://acm.hdu.edu.cn/showproblem.php?pid=6514 题意 给出一个大矩形(\(nm\leq10^7\)),有p个矩形覆盖,然后有q次询问,询问指定矩形内是否覆 ...
- tomcat参数java_opts调整
启动文件修改 在windows环境下,tomcat下的~/bin/catalina.bat文件,在文件头部加入: set "JAVA_OPTS=%JAVA_OPTS% -server -Xm ...
- 【Linux命令】Linux命令后面所接选项和参数的区别
Linux命令后面所接选项和参数的区别 在使用Linux命令时,有时候后面会跟一些"选项"(options)或"参数"(agruments) 命令格式为: #中 ...
- 洛谷 P2656 (缩点 + DAG图上DP)
### 洛谷 P2656 题目链接 ### 题目大意: 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖 ...
- pixijs shader 扫光加强版
pixijs shader 扫光加强版 const app = new PIXI.Application({ transparent: true }); document.body.appendChi ...
- UVA 10790 How Many Points of Intersection? 组合数学
We have two rows. There are a dots on the top row and b dots on the bottom row. We draw line segment ...
- python常用库简单使用( PyPDF2 )
PyPDF2学习 1 这个模块的名字对大小写是敏感的,所以,确保y是小写的,其他字母都是大写的
- 使用Python+Selenium模拟登录QQ空间
使用Python+Selenium模拟登录QQ空间爬QQ空间之类的页面时大多需要进行登录,研究QQ登录规则的话,得分析大量Javascript的加密解密,这绝对能掉好几斤头发.而现在有了seleniu ...