leetcode 113 path Sum II 路径和

递归先序遍历+vector<int>容器记录路径
/**
* 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<int> out;
vector<vector<int>> res;
dfs(root,sum,,out,res);
return res;
}
//还是递归先序遍历+vector<int>容器记录路径
void dfs(TreeNode* node,int sum,int curSum,vector<int> &out,vector<vector<int>> &res){
if(!node) return;
curSum+=node->val;
out.push_back(node->val);
if(node->left==NULL&&node->right==NULL&&curSum==sum) res.push_back(out);
dfs(node->left,sum,curSum,out,res);
dfs(node->right,sum,curSum,out,res);
out.pop_back();
}
};
leetcode 113 path Sum II 路径和的更多相关文章
- [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 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 give ...
- 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 ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [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 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 113 Path Sum II 路径总和 II
给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22, 5 / \ 4 ...
随机推荐
- vue-cli3.0中使用 postcss-pxtorem
vue.config.js module.exports = { lintOnSave: true, css: { loaderOptions: { postcss: { plugins: [ req ...
- python实现建造者模式
python实现建造者模式 前言 无论是在现实世界中还是在软件系统中,都存在一些复杂的对象,它们拥有多个组成部分,如汽车,它包括车轮.方向盘.发送机等各种部件.而对于大多数用户而言,无须知道这些部件的 ...
- initlocation - 创建一个从属的 PostgreSQL数据库存储区
SYNOPSIS initlocation directory DESCRIPTION 描述 initlocation 创建一个新的PostgreSQL从属数据库存储区.参阅 CREATE DATAB ...
- :OpenCV人脸识别Fisherface算法源码分析
https://blog.csdn.net/loveliuzz/article/details/73875904
- PAT Basic 1038 统计同成绩学生 (20 分)
本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第 1 行给出不超过 1 的正整数 N,即学生总人数.随后一行给出 N 名学生的百分制整数成绩,中间以空格分隔.最 ...
- MySQL发生系统错误2 系统无法找到指定文件
https://blog.csdn.net/digitalmon/article/details/78152187 https://www.cnblogs.com/gaogaoyanjiu/p/104 ...
- ACM常用之 异或运算的性质。
- Gym-100923H-Por Costel and the Match(带权并查集)
链接: https://vjudge.net/problem/Gym-100923H 题意: Oberyn Martell and Gregor Clegane are dueling in a tr ...
- javaScript中的 this
普通函数中的 this // es3中 function foo() { console.log(this);// 这里的this是 window } foo(); // 在es5中 严格模式下 fu ...
- js对象的创建模式
方式一: Object构造函数模式 * 套路: 先创建空Object对象, 再动态添加属性/方法 * 适用场景: 起始时不确定对象内部数据 * 问题: 语句太多 /* 一个人: name:" ...