path-sum-ii leetcode C++
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example: Given the below binary tree andsum = 22, 5 /
4 8 / /
11 13 4 / \ /
7 2 5 1 return
[ [5,4,11,2], [5,8,4,5] ]
C++
/**
* Definition for binary tree
* 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>> dp;
vector<int> path;
getPath(root,sum,dp,path);
return dp;
}
void getPath(TreeNode *root, int sum,vector<vector<int>>& dp,vector<int> path){
if(NULL == root) return;
path.push_back(root->val);
if(NULL == root->left && NULL == root->right && root->val == sum )
dp.push_back(path);
getPath(root->left,sum - root->val,dp,path);
getPath(root->right,sum - root->val,dp,path);
}
};
path-sum-ii leetcode C++的更多相关文章
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- Path Sum II leetcode java
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- Path Sum II——LeetCode
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
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [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 ...
- 【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 ...
随机推荐
- 树莓派的kodi设置遥控器的方法
首先你需要买一个红外接收器,根据卖家的文档,插到树莓派的GPIO串口上, 我的红外接收器是18入口,17出口, 所以我的config.txt文件设置如下 dtoverlay=lirc-rpi,gpio ...
- html2canvas实现截取指定区域或iframe的区域
官网文档: http://html2canvas.hertzen.com/ 使用的是 jquery 3.2.1 html2canvas 1.0.0-rc.7 截取根据id的指定区域: var ca ...
- PKI及SSL协议分析PKI及SSL协议分析
任务一:搭建CA服务器 本任务初步了解CA服务器的原理和配置过程.操作都在CA服务器上. 1.远程桌面方式登录到CA服务器,在CMD下查看本机IP地址: 2.安装证书服务 依次点击:"开始& ...
- 接口测试-Mock测试方法
接口测试-Mock测试方法一.关于Mock测试1.什么是Mock测试?Mock 测试就是在测试过程中,对于某些不容易构造(如 HttpServletRequest 必须在Servlet 容器中才能构造 ...
- P6295-有标号 DAG 计数【多项式求逆,多项式ln】
正题 题目链接:https://www.luogu.com.cn/problem/P6295 题目大意 求所有\(n\)个点的弱联通\(DAG\)数量. \(1\leq n\leq 10^5\) 解题 ...
- P4884-多少个1?【BSGS】
正题 题目链接:https://www.luogu.com.cn/problem/P4884 题目大意 求一个最小的\(n\)使得\(n\)个连续的\(1\)其在模\(m\)意义下等于\(k\). \ ...
- 让前端的下拉框支持单选、多选及全选,后台MyBaits解决方案
目录 一.解决思路 二.请求参数 三.后台相关代码 四.Mybatis注意要点 一.解决思路 让前端的下拉框支持单选.多选及全选,后台让Mybatis使用** trim **标签拼接动态SQL,实 ...
- Spring技术内幕笔记2--我懒不写了哈哈哈哈。
目录 1.1 关于IOC容器设计的线路区别 1.1.1 BeanFactory 1.1.2 ApplicationContext 2.1 FileSystemXmlApplicationContext ...
- LuoguP1557 Kruscal的加法 题解
题目Link 就是这道题,做了我整整一天! 看到题目,首先想到的就是:就这?就这一道大水题也能是绿?然后十分钟写完代码,提交-- 果不其然,绿题不是白绿的,看了一眼数据和讨论,又得写高精了-- 先附上 ...
- MYSQL小版本升级(5.7.21至5.7.25)
1.环境确认 [root@mysql ~]# ps -ef |grep -i mysql root 9173 1 0 2020 ? 00:00:00 /bin/sh /mysql/data/mysql ...