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

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. HDU 1074 Doing Homework(像缩进DP)

    Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...

  2. Codeforces Round #257 (Div. 2/A)/Codeforces450A_Jzzhu and Children

    解题报告 没什么好说的,大于m的往后面放,,,re了一次,,, #include <iostream> #include <cstdio> #include <cstri ...

  3. js中的open和showModalDialog

    一.window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法:window.open(pageURL,name,p ...

  4. 网络资源(4) - extJS视频

    2014_08_24 http://v.youku.com/v_show/id_XMjk2ODc0MjA4.html?f=7183617 extJS视频教程04——ExtJS框架入门

  5. java字节中的基本类型的职业的数目 (采访总是问)

    因为移动装置存储器中的移动开发的局限性,数据的字节数需要考虑往往在占领中使用的类型. 下面介绍下一个Java,以加深记忆. 在Java中一共同拥有8种基本数据类型,当中有4种整型,2种浮点类型,1种用 ...

  6. oracle_查询Oracle正在执行和执行过的SQL语句

    ---正在执行的select a.username, a.sid,b.SQL_TEXT, b.SQL_FULLTEXT  from v$session a, v$sqlarea b where a.s ...

  7. 无废话WCF入门教程三[WCF的宿主]

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  8. hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场

    Parenthese sequence                                                                     Time Limit: ...

  9. Swift语言指南(六)--可选值

    原文:Swift语言指南(六)--可选值 在值可能不存在的情况下使用可选值(optional), 可选值是: · 存在一个值,这个值等于 x 或 · 不存在任何值 注: 在 C 和 Objective ...

  10. JqueryAjax异步加载在ASP.NET

    前台代码 <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript">< ...