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的更多相关文章

  1. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  3. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  4. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  5. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

  6. [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用

    [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...

  7. leetcode 38:path-sum

    题目描述 给定一个二叉树和一个值sum,判断是否有从根节点到叶子节点的节点值之和等于sum的路径, 例如: 给出如下的二叉树,sum=22,              5              / ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. windows已安装solr

    下载地址:http://archive.apache.org/dist/lucene/solr/ 操作环境:  Win7,Tomcat6, Solr4.3, Jdk6 下载solr4.3的包,解压到本 ...

  2. MIFARE系列6《射频卡与读写器的通信》

    1. ATR(Answer to request) 读写器呼叫磁场内的卡片.卡片对呼叫做出应答. 对刚进入磁场得到电复位处于休闲状态的卡片,卡请求(REQA,0x26):对于已进行过读写操作并进入休眠 ...

  3. uva 11987 Almost Union-Find (并检查集合)

    标题效果: 三操作. 1. 合并两个集合 2.代替所述第二组的第一个元素 3.输出设置数量,并.. IDEAS: 使用p该元素的记录数,其中集合,建立并查集. #include <cstdio& ...

  4. The Swift Programming Language-官方教程精译Swift(9) 枚举-- --Enumerations

    枚举定义了一个通用类型的一组相关的值,使你可以在你的代码中以一个安全的方式来使用这些值.   如果你熟悉 C 语言,你就会知道,在 C 语言中枚举指定相关名称为一组整型值.Swift 中的枚举更加灵活 ...

  5. CII-原子

    <atom.h> #ifndef ATOM_INCLUDED #define ATOM_INCLUDED extern int Atom_length(const char *str); ...

  6. javascript 验证 yyyy-MM-dd HH:mm:ss 的正则表达式

    原文:javascript 验证 yyyy-MM-dd HH:mm:ss 的正则表达式 ^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13 ...

  7. Chapter 1 Securing Your Server and Network(1):选择SQL Server业务经理

    原版的:http://blog.csdn.net/dba_huangzj/article/details/37924127  ,专题文件夹:http://blog.csdn.net/dba_huang ...

  8. IOC 容器初始化

    WebApi 插件式构建方案:IOC 容器初始化 一般来说,一个现代化的网站加载流程是这样的:程序集加载后,我们会初始化 IOC 容器,以便于接下来解析对象用. 我们插件式的开发,这一步更为重要.这是 ...

  9. leetcode[55] Merge Intervals

    题目:给定一连串的区间,要求输出不重叠的区间. Given a collection of intervals, merge all overlapping intervals. For exampl ...

  10. leetcode第35题--Valid Sudoku

    题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...