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 ...
随机推荐
- HDU Group
Group 题目: 给出n个数,是1-n的排列.要求你每次给你一个区间求出这个区间能够被分成的小区间个数. 一个不连续的数能够被分成一个小区间.t-1,t或t,t+1表示连续. 算法: 高速做法应该是 ...
- BZOJ 1004 HNOI2008 Cards Burnside引理
标题效果:特定n张卡m换人,编号寻求等价类 数据保证这m换人加上置换群置换后本身构成 BZOJ坑爹0.0 条件不那么重要出来尼玛怎么做 Burnside引理--昨晚为了做这题硬啃了一晚上白书0.0 都 ...
- MongoDB学习笔记<两>
继续有shell学问,他们继续研究的例子,下面的知识: --文档数据插入 --文档数据删除 --文档数据更新 如下面的详细信息: 1.插入文档 db.person.insert({"name ...
- Responsive Design in 3 Steps
Responsive web design is no doubt a big thing now. If you still not familiar with responsive design, ...
- POJ 1637 Sightseeing tour(最大流)
POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...
- winsock2之最简单的win socket编程
原文:winsock2之最简单的win socket编程 server.cpp #include <WINSOCK2.H> #include <stdio.h> #pragma ...
- 【转】Oracle修改表空间为自动扩展
1.数据文件自动扩展的好处1)不会出现因为没有剩余空间可以利用到数据无法写入2)尽量减少人为的维护3)可以用于重要级别不是很大的数据库中,如测试数据库等 2.数据文件自动扩展的弊端1)如果任其扩大,在 ...
- 模板专业化和模板偏特样片(template specialization and partial template specialization)
测试环境: win7 64 g++ 4.8.1 /*************************************************************************** ...
- Fluent NHibernate
Fluent NHibernate]第一个程序 目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Flu ...
- 19.最经济app发短信的方法
在创业团队.一个重要的原则是能省就省,该花的花,明智地使用金钱. 今的app,为了获取用户的社交关系.须要用户的手机号注冊. 用手机号注冊就涉及到一个发送短信验证码的问题,那怎么才干在短信服务上投入最 ...