【leetcode】Path Sum2
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 and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
解体思路:利用递归深度优先遍历,保存中间结果。
/**
* 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> > result;
vector<int> intermediate;
dfs(root, sum, intermediate, result);
return result;
} void dfs(TreeNode *root, int gap, vector<int> &intermediate,
vector<vector<int> > &result){
if(root == nullptr)
return;
intermediate.push_back(root->val); if(root->left == nullptr && root->right == nullptr){
if(gap == root->val)
result.push_back(intermediate);
} dfs(root->left, gap - root->val, intermediate, result);
dfs(root->right, gap - root->val, intermediate, result); intermediate.pop_back();//弹出回退
}
};
【leetcode】Path Sum2的更多相关文章
- 【leetcode】Path Sum IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【leetcode】Path Sum
题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- 【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 I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径
在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 【LeetCode】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】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- Java技术——String类为什么是不可变的
0. 前言 如果一个对象,在它创建完成之后不能再改变它的状态,包括对象内的成员变量.基本数据类型的值等等.那么这个对象就是不可变的.众所周知String类就是不可变的.转载请注明出处为SEU_Ca ...
- 《图说VR入门》——360全景视频
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/53674647 作者:car ...
- python中的-1
-1单个使用时表示最后一个: >>> [1,2,3,4][-1] 4 表示范围(区间)时,因为是开区间表示方法,如[0:2]是不包括2的所以 [0:-1]只能访问到倒数第二个(不包括 ...
- scala学习——(1)scala基础(下)
(七)定长数组 val array_name = new Array[T](length) val array_name = Array("","") 通过() ...
- CS100.1x Introduction to Big Data with Apache Spark
CS100.1x简介 这门课主要讲数据科学,也就是data science以及怎么用Apache Spark去分析大数据. Course Software Setup 这门课主要介绍如何编写和调试Py ...
- 2、Arx二次开发创建第一个应用程序
一.本节课程 Arx二次开发创建第一个应用程序 二.本节要讲解的知识点 1.手动创建ARX的应用的步骤. 2.应用向导创建ARX应用程序的步骤. 三.具体内容 1.需求:创建一个Hello World ...
- Ubuntu16.04+Java8+Mysql5.7+Tomcat8.5服务器环境配置
本文章使用环境: Ubuntu16.04 Java8 Mysql5.7 Tomcat8.5 Ubuntu 版本16.04, 本文采用SSH远程登录主机 工具:Xshell 默认使用root用户登录 ( ...
- PAT甲题题解-1002. A+B for Polynomials (25)-多项式相加
注意两点:1.系数也有可能加起来为负!!!一开始我if里面判断为>0导致有样例没过...2.如果最后所有指数的系数都为0,输出一个0即可,原本以为是输出 1 0 0.0... #include ...
- Scrum Meeting NO.2
Scrum Meeting No.2 1.会议内容 今天,我们对已经确定的任务进行了分配,并针对界面设计方面的细节进行讨论. 由于这周其它课程任务繁重(编译+数据库).前端的任务主要分配给编程能力较好 ...
- (Alpha)Let's-展示博客
Let's Alpha 项目答辩 ·选题由来 手机端——用户相对较多,使用环境限制相对宽松 手机游戏?校园p2p应用?线下交流!(滴滴打水?) 模式的选择:发起——加入活动 ...