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

分析:

这题并不难。其实就是深搜。

就是可能一开始,觉得函数递归的时候,要传哪些参数下去?要返回那些值??

当有多个返回值的时候怎么办??

Trick:  C++ 中可以采用引用,来返回值的功能。

1. 返回值: 多条 paths, 可以将 vector<vector<int> >& result 作为形参;  这样其实 所有的递归函数都共享了这个 vector; 当找到路径时,push_back即可。

2. 需要往下传的参数: sum,   当前的路径;

/**
* 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) {
vector<vector<int> > result;
vector<int> path; MyPathSum(root, sum, path, result); return result;
}
private:
void MyPathSum(TreeNode *root, int sum, vector<int> &path, vector<vector<int> > &result)
{
if(root == NULL) return; path.push_back(root->val);
if(root->left == NULL && root->right == NULL){
if(sum == root->val){
result.push_back(path);
}
} if(root->left){
MyPathSum(root->left, sum - root->val, path, result);
} if(root->right){
MyPathSum(root->right, sum - root->val, path, result);
}
path.pop_back();
}
};

LeetCode---- 二叉树中,找出和为某值的所有路径的更多相关文章

  1. (转)在二元树中找出和为某一值的所有路径,java版本

    摘自:http://www.cnblogs.com/qi09/archive/2011/05/24/2055643.html 4.在二元树中找出和为某一值的所有路径 题目:输入一个整数和一棵二元树.  ...

  2. 用C#写一个函数,在一个数组中找出随意几个值相加等于一个值 与迭代器对比

    算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}  要找出那些数相加等 ...

  3. [leetcode]270. Closest Binary Search Tree Value二叉搜索树中找target的最接近值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  4. leetcode 二叉搜索树中第K小的元素 python

          二叉搜索树中第K小的元素     给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...

  5. LeetCode——Single Number(找出数组中只出现一次的数)

    问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  6. TF-IDF与余弦相似性的应用(二):找出相似文章

    上一次,我用TF-IDF算法自动提取关键词. 今天,我们再来研究另一个相关的问题.有些时候,除了找到关键词,我们还希望找到与原文章相似的其他文章.比如,"Google新闻"在主新闻 ...

  7. leetcode 双周赛9 找出所有行中最小公共元素

    给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [[,,,,] ...

  8. Leetcode33--->Search in Rotated Sorted Array(在旋转数组中找出给定的target值的位置)

    题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might becom ...

  9. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

随机推荐

  1. javascript常用函数(1):jquery操作select 基本操作

    $(this).children('option:selected').val();//这就是selected的值 $("#charCity").empty();//内容清空: j ...

  2. WMI远程访问问题解决方法

    WMI 全称为:Microsoft Windows Management Instrumentation (WMI)  按微软的介绍大致如下:      WMI 是 Microsoft 主要的针对 W ...

  3. MVC之视图的布局

      1. RenderBody  布局在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到标签里有这样一条语句 ...

  4. 关于gridview 实现查询功能的方法

    protected void btnSearch_Click(object sender, EventArgs e) { TestCon(); } protected void btnAllData_ ...

  5. MATLAB 矩阵转化为灰度图

    A=[ 1.00 0.96 0.98 0.88 0.94 0.61 0.96 0.80 0.98 0.89 0.96 1.00 0.94 0.90 0.95 0.71 0.96 0.83 0.90 0 ...

  6. for循环练习——7月23日

    练习一:输入一个整数,求从1到这个数的累加和 //练习1:输入一个整数,计算从1加到这个数的结果 Console.Write("请输入一个正整数:"); int a = int.P ...

  7. cookie与sessionID之间的关系实验

    上一篇介绍了cookie,这里来看看cookie与sessionID之间有什么关系. 一.编写测试用例代码 新建一个servlet如下: public class SessionServlet ext ...

  8. biztalk中使用WCF-SQL接受传送数据【转】

    接触biztalk时间不长,转载一篇学习教程: http://www.cnblogs.com/chnking/archive/2010/05/09/1731098.html chnking写的. 一. ...

  9. 学习记录013-NFS相关知识点

    一.NFS相关知识点 1.NFS常用的路径/etc/exports NFS服务主配置文件,配置NFS具体共享服务的地点/usr/sbin/exportfs NFS服务的管理命令,exportfs -a ...

  10. PHP Memcached应用实现代码

    一.memcached 简介 在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东.这里简单介绍一下,memcached 是高 ...