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) 有关 这种情况下,时间 ...
随机推荐
- Unity编辑器扩展学习 转载
https://www.xuanyusong.com/archives/category/unity/unity3deditor 1 using UnityEngine; public class T ...
- vue项目搭建介绍02
目录 vue项目搭建介绍02 python-pycharm设置: vue创建项目分类: vue-cli构建 自定义构建 基础的vue项目目录: vue项目搭建介绍02 python-pycharm设置 ...
- hw笔试题-02
#include<stdio.h> #include<string.h> typedef struct { char *mem; char len; }m_table_t; i ...
- sql 合并结果集数据
起因:项目上查询一些数据,需要将查询后的结果合并到一列中. 1.STUFF函数 官方api: https://docs.microsoft.com/zh-cn/sql/t-sql/functions/ ...
- MSM8909中LK阶段LCM屏适配与显示流程分析(一)
1.前言 在驱动开发中,我们往往需要适配一些新的屏幕或者调试一些屏幕的参数等,对于Qualcomm的MSM8909这款SoC,当启动Android系统时,会有一个LK阶段,该阶段用来启动Linux内核 ...
- 在Visual Studio 中使用 <AutoGenerateBindingRedirects> 来解决引用的程序集版本冲突问题
问题: https://stackoverflow.com/questions/42836248/using-autogeneratebindingredirects-in-visual-studio ...
- 微信小程序navigator页面跳转失效原因
在编写小程序时遇到一个问题:使用 <navigator url='/pages/lists/index'>...</navigator>进行跳转没有反应.控制台也没有报错,ap ...
- python3测试网站网速
一.运行环境 1.Windows 10 2.python 3.8 二.安装第三方库pycurl 1.先安装 pip install wheel 2.在安装pycurl https://download ...
- 读 HeadFirst 设计模式
以往虽也看过相关设计模式的书籍,但能和与HeadFirt设计模式这本书相比不是缺乏严谨性就是缺乏具体应用实例,还有幽默生动以及引人启发的观点. 设计原则 设计原则并不能光靠死记硬背,我们需要通过具体的 ...
- Qt程序app添加图标复制到其它电脑后不显示的解决方法
原因: 主是要因为Qt显示图标需要依赖一些库来进行转换,而复制到其它电脑后不显示,是没有复制相应的库所致,所以把相应库复制过去就行了. 复制Qt的plugins目录下的imageformats文件到程 ...