上一题类似,这里是要记录每条路径并返回结果。

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: void pathSum(TreeNode *root, vector<vector<int> > &ans, vector<int> tmp, int subsum, int sum)
{
if (!root) return ;
if (!root -> left && !root -> right && subsum + root -> val == sum)
{
tmp.push_back(root -> val);
ans.push_back(tmp);
} if (root -> left)
{
tmp.push_back(root -> val);
subsum += root -> val;
pathSum(root -> left, ans, tmp, subsum, sum);
tmp.pop_back(); //因为判断右子树的时候不需要左子树的和
subsum -= root -> val;
}
if (root -> right)
{
tmp.push_back(root -> val);
subsum += root -> val;
pathSum(root -> right, ans, tmp, subsum, sum);
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum)
{
vector<vector<int> > ans;
vector<int> tmp; pathSum(root, ans, tmp, , sum);
return ans;
}
};

其实效率好一些的是对tmp传入引用,例如vector<int> &tmp,那么此时每次记录结果或者左右递归之后都要有一个pop值,来保证tmp符合当前的要求:详见

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: void pathSum(TreeNode *root, vector<vector<int> > &ans, vector<int> &tmp, int subsum, int sum)
{
if (!root) return ;
if (!root -> left && !root -> right && subsum + root -> val == sum)
{
tmp.push_back(root -> val);
ans.push_back(tmp);
tmp.pop_back(); // 保持tmp
} if (root -> left)
{
tmp.push_back(root -> val);
subsum += root -> val;
pathSum(root -> left, ans, tmp, subsum, sum);
tmp.pop_back(); // 因为判断右子树的时候不需要左子树的和
subsum -= root -> val; // 同上理
}
if (root -> right)
{
tmp.push_back(root -> val);
subsum += root -> val;
pathSum(root -> right, ans, tmp, subsum, sum);
tmp.pop_back(); // 保持tmp
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum)
{
vector<vector<int> > ans;
vector<int> tmp; pathSum(root, ans, tmp, , sum);
return ans;
}
};

leetco Path Sum II的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

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

  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. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

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

  7. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  8. Path Sum,Path Sum II

    Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...

  9. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

随机推荐

  1. imagick获取图片的大小bug

    <? php /* imagick的获取图片的高度和宽度函数有问题,使用GD函数可获得正确结果 gd函数 array getimagesize ( string $filename [, arr ...

  2. Cordic 算法的原理介绍

    cordic 算法知道正弦和余弦值,求反正切,即角度. 采用用不断的旋转求出对应的正弦余弦值,是一种近似求解发. 旋转的角度很讲求,每次旋转的角度必须使得 正切值近似等于 1/(2^N).旋转的目的是 ...

  3. 接收一个IT招聘促销信息,试着想参加,有兴趣的可以携手并进,共同。

    时隆重举行! 招聘的企业: (个人认为,中智.也买酒还是非常有吸引力的) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHdia2Zj/font/5a6L ...

  4. 纯js客服插件集qq、旺旺、skype、百度hi、msn

    原文 纯js客服插件集qq.旺旺.skype.百度hi.msn 客服插件,集qq.旺旺.skype.百度hi.msn 等 即时通讯工具,并可自己添加支持的通讯工具,极简主义,用法自己琢磨.我的博客 h ...

  5. mysql只导出表结构或数据

    唯一的非导电结构指南数据 mysqldump -t 数据库名称 -uroot -p > xxx.sql 指南结构不仅指导数据 mysqldump    --opt -d  数据库名 -u -p ...

  6. DFS-hdu-2821-Pusher

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2821 题目意思: 给一个n*n的矩阵,里面有些位置是空的,有些位置有箱子(a代表一个箱子,b代表两个 ...

  7. 【从翻译mos文章】oracle linux 和外部存储系统 关系

    oracle  linux 和外部存储系统 关系 参考原始: Oracle Linux and External Storage Systems (Doc ID 753050.1) 范围: Linux ...

  8. java_log4j 经典配置

    程序加载制定日志文件 public static final String log4j = "log4j.xml"; /** * @declare 加载log4j * @throw ...

  9. android 编译共享ccache的缓存

    1. android自带的ccache版本号(2.4版本号)过低,是无法支持以上的功能的,须要使用新版ccache. 2. 最新的ccache请到http://ccache.samba.org/dow ...

  10. SOA、REST 和六边形架构

    SOA.REST 和六边形架构 上一篇:<IDDD 实现领域驱动设计-架构之经典分层> 阅读目录: SOA-面向服务架构 REST 与 RESTful 资源(Resources) 状态(S ...