LeetCode:Path Sum

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.

递归求解,代码如下:

 /**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL)return false;
if(root->left == NULL && root->right == NULL && root->val == sum)
return true;
if(root->left && hasPathSum(root->left, sum - root->val))
return true;
if(root->right && hasPathSum(root->right, sum - root->val))
return true;
return false;
}
};

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 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> > res;
if(root == NULL)return res;
vector<int> curres;
curres.push_back(root->val);
pathSumRecur(root, sum, curres, res);
return res;
}
void pathSumRecur(TreeNode *root, int sum, vector<int> &curres, vector<vector<int> >&res)
{
if(root->left == NULL && root->right == NULL && root->val == sum)
{
res.push_back(curres);
return;
}
if(root->left)
{
curres.push_back(root->left->val);
pathSumRecur(root->left, sum - root->val, curres, res);
curres.pop_back();
}
if(root->right)
{
curres.push_back(root->right->val);
pathSumRecur(root->right, sum - root->val, curres, res);
curres.pop_back();
}
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440044.html

LeetCode:Path Sum I II的更多相关文章

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

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

  3. [Leetcode][JAVA] Path Sum I && II

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

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

  5. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

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

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

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

随机推荐

  1. LINQ SQL分组取最近一条记录

    最近项目有一个需求,从订单表查询出每个客户最近一条订单记录.数据库表结构如下图 SELECT * FROM ( select ROW_NUMBER()over(partition by [custid ...

  2. JavaScript字符串函数大全

    JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...

  3. java微信接口之四—上传素材

    一.微信上传素材接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=A ...

  4. High Aavialability with Group Replication-by宋利兵

    今天,2016年12月12日, Group Replication GA啦!GA的版本是MySQL-5.7.17. 下载连接如下: http://dev.mysql.com/downloads/mys ...

  5. JavaScript中typeof的坑

    typeof是一个运算符,它对操作数返回的结果是一个字符串,有6种(只针对ES,不包含HOST环境对象) 'undefined' 'boolean' 'string' 'number' 'object ...

  6. CI 框架中 AR 操作

    Model 层中的部分代码 /** * CI 中的 AR 操作 * @author zhaoyingnan **/ public function mAR() { /*************** 查 ...

  7. splice()函数,'SPLICE_F_MOVE' 'SPLICE_F_NONBLOCK' 'SPLICE_F_MORE' undeclared

    1.编译含有splice()函数的程序时出现,'SPLICE_F_MOVE'  undeclared,'SPLICE_F_NONBLOCK' ‘SPLICE_F_MORE' 也是一样undeclare ...

  8. C自学笔记-递归与迭代的使用方法笔记与两者的使用场合

    递归和迭代在刚开始学C语言的时候就学过,但是不知道怎么使用.今天遇到一个题目分析过后 我瞬间想起来之前学过递归的方法,做完题后顺便翻了翻书整理了这个笔记.题目大概是这样的. 题目:猴子吃桃问题:猴子第 ...

  9. [Hadoop]如何安装Hadoop

    Hadoop是一个分布式系统基础架构,他使得用户可以在不了解分布式底层细节的情况下,开发分布式程序. Hadoop的重要核心:HDFS和MapReduce.HDFS负责储存,MapReduce负责计算 ...

  10. hdu-5895 Mathematician QSC(数学)

    题目链接: Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Jav ...