Leetcode::Pathsum & Pathsum II
Pathsum
Description:
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum =22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
分析:二叉树root-to-leaf的查找,深搜搜到结果返回即可
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if(root==NULL) return false;
int sumval = root->val;
if(sumval==sum && root->left==NULL &&root->right==NULL) return true;
else{
return (hasPathSum(root->left,sum-sumval)||hasPathSum(root->right,sum-sumval));
}
}
};
PathsumII
Description:
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]
]
分析:这一题和上一题的区别在于不仅需要判断有没有,还需要记录是什么。在深搜的时候维护一个stack(这里用vector实现),记录已经走过的路径,
返回是栈也弹出相应节点
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool pathrec(vector<vector<int> > &rec, vector<int>& path,TreeNode* r,int sum)
{
sum-=r->val;
//if(sum<0) return false;
//Tell if it's a leaf node
if(r->left ==NULL && r->right == NULL)
{
if(sum!=) return false;
else{
vector<int> one = path;
one.push_back(r->val);
rec.push_back(one);
return true;
}
}
//Ordinary node
path.push_back(r->val);
bool flag = false;
if(r->left!=NULL) pathrec(rec,path,r->left,sum);
if(r->right!=NULL) pathrec(rec,path,r->right,sum);
path.erase(path.end()-);
return flag; }
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > rec;
if(root==NULL) return rec;
vector<int> path;
pathrec(rec,path,root,sum); return rec;
}
};
Leetcode::Pathsum & Pathsum II的更多相关文章
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
- [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用
[LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...
- leetcode 38:path-sum
题目描述 给定一个二叉树和一个值sum,判断是否有从根节点到叶子节点的节点值之和等于sum的路径, 例如: 给出如下的二叉树,sum=22, 5 / ...
- [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 II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
随机推荐
- Controller与Action
Controller与Action 我们知道在MVC5和之前的版本,两个框架的生命周期是不一样的,在新版MVC6中,MVC Controller/Web API Controller已经合二为一了,本 ...
- 使用Json让Java和C#沟通的方法
原文:使用Json让Java和C#沟通的方法 最近很忙啊,新项目下来了,都没时间写博客了.频率降低点,但不能不总结跟大家分享啊. 我们在项目里经常要涉及到各模块间的通信,这其中又不可避免要碰到各类语言 ...
- 7.Swift翻译教程系列——控制循环的流程
英语PDF下载链接http://download.csdn.net/detail/tsingheng/7480427 Swift提供了类C语言类似的控制流结构.包含for循环和while循环来多次运行 ...
- 持续交付工具ThoughtWorks Go部署step by step
持续交付工具ThoughtWorks Go部署step by step http://blogs.360.cn/360cloud/2014/05/13/%E6%8C%81%E7%BB%AD%E4%BA ...
- ARM指令集中经常使用的存储和载入指令
ARM微处理器支持载入/存储指令用于在寄存器和存储器之间传送数据,载入指令用于将存储器中的数据传送到寄存器,存储指令则完毕相反的操作.经常使用的载入存储指令例如以下: - LDR 字数据载入 ...
- 收集的VS2013的使用小技巧( 不断总结中)
对于经常使用vs的朋友,如果能用键盘直接做的事,还是键盘更便捷点,现在我就把自己遇到的一些给写下来. 1.对一个函数的说明 先写一个函数,以及参数,完成后,在函数上输入///,vs会自动补全说明的信息 ...
- Java判断当前用户数及当前登录用户数工具类-session原理
JavaWeb开发中,有时会遇到统计或管理用户登录数或者当前在线多少用户,分别都是谁的情况.当然,实现途径多种多样.下面列举一下通过session实现的一种统计. public class MySes ...
- 最新HTML BroadcastChannel API引荐
HTML BroadcastChannel API 当前浏览器中只有Firefox38唯一能支持BroadcastChannel API(在编写本文的时间点),而Firefox38官方宣称要到2015 ...
- OC的构造方法与分类知识点总结
OC语言构造方法: 用来初始化对象的方法,对象方法,以减号开头 为了让对象创建出来,成员变量就会有一些固定的值 重写构造方法的注意点: 先调用父类的构造方法([super init]) 再进行子类内部 ...
- 第4章2节《MonkeyRunner源码剖析》ADB协议及服务: ADB服务SERVICES.TXT翻译参考(原创)
天地会珠海分舵注:本来这一系列是准备出一本书的,详情请见早前博文“寻求合作伙伴编写<深入理解 MonkeyRunner>书籍“.但因为诸多原因,没有如愿.所以这里把草稿分享出来,所以错误在 ...