【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 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<int> tmp;
vector<vector<int> > result;
if(root==NULL)
{
return result;
}
DFS(root,sum,tmp,result);
return result; } void DFS(TreeNode *root,int &sum, vector<int> tmp,vector<vector<int> > &result,int cur=)
{
if(root==NULL)
{
return;
} int val=root->val;
tmp.push_back(val); if(root->left==NULL&&root->right==NULL)
{
if(cur+val==sum) result.push_back(tmp);
return;
}
DFS(root->left,sum,tmp,result,cur+val);
DFS(root->right,sum,tmp,result,cur+val);
}
};
【leetcode】Path Sum II的更多相关文章
- 【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 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【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 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】【Medium】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
题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
随机推荐
- Orchard源码分析(2):Orchard.Web.MvcApplication类(Global)
概述 分析一个的ASP.NET项目源码,首先可以浏览其项目结构,大致一窥项目其全貌,了解项目之间的依赖关系.其次可以浏览Web.config和Global.asax文件,找到应用程序的入口点. 本 文 ...
- [c#]params可变参数
摘要 在项目中多多少少会用到params这个关键字,来修饰参数,它的作用,让该参数的个数是可变的,并且可变参数必须是方法的最后一个参数.但如何判断到底有没有为该参数传递值,怎么判断? 一个例子 sta ...
- struts2文件上传提示信息国际化
1.在src的目录下新建文件fileUpload.properties 如图: fileUpload.properties文件内容为(把英文提示自定义为中文提示) struts.messages.er ...
- [原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- EF-error 0152: No Entity Framework provider found...
突然就报这个错了... ... 哈哈··· 原来是 "EntityFramework.SqlServer.dll" 没有引用··· 添加引用就好了... ... 还好不了?那就不知 ...
- ASP.NET Core -- 安装版
首先安装的→Visual Studio Community 原文地址 本来觉得安装了vs 就可以了,,结果一直报错,,,太天真... 接下来还要安装 →NuGet.Tools.vsix →DotNet ...
- codevs2171 棋盘覆盖
题目描述 Description 给出一张n*n(n<=100)的国际象棋棋盘,其中被删除了一些点,问可以使用多少1*2的多米诺骨牌进行掩盖. 输入描述 Input Description 第一 ...
- POJ 2486 Apple Tree
好抽象的树形DP......... Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6411 Accepte ...
- Window环境下搭建MyEclipse+Tomcat+MAVEN+SVN
1.JDK的安装 首先下载JDK,这个从sun公司官网(http://www.oracle.com/)可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置 ...
- 第31天 mvp
interactor调用接口 Activity包含Presenter,这样界面上的操作就会通知到Presenter. Presenter调用view接口, Activity实现view接口,这样Pre ...