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

给定一个二叉树和数字sum,输出二叉树中从根节点到叶子节点全部路径中和等于sum的路径。

每经过路径上的一个节点。记录这个节点值到temp变量中,用sum减去这个节点值得到一个新的sum值,用这个新的sum值去处理节点的非空左右子树,当节点为叶子节点且sum等于节点值时。说明到了路径终点且路径经过的节点和恰好等于sum,此时记录temp到结果数组res中。

处理过程中使用引用传參的方式传递temp和res变量。省却耗时的參数复制操作。由于是引用传递。节点在temp运行push_back操作并对其非空左右子树递归调用函数完毕后,须要对temp进行pop_back操作。清除自己填入的节点值,这样才干不影响后面程序的处理。

AC code:

/**
* 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<int> temp;
vector<vector<int>> res;
helper(root,sum,temp,res);
return res;
}
void helper(TreeNode *root,int sum,vector<int>& temp,vector<vector<int>> &res)
{
if(root==NULL)
return;
if(root->left==NULL&&root->right==NULL&&sum==root->val)
{
temp.push_back(root->val);
res.push_back(temp);
temp.pop_back();
return;
}
if(root->left!=NULL)
{
temp.push_back(root->val);
helper(root->left,sum-root->val,temp,res);
temp.pop_back();
}
if(root->right!=NULL)
{
temp.push_back(root->val);
helper(root->right,sum-root->val,temp,res);
temp.pop_back();
}
}
};

leetcode 刷题之路 66 Path Sum II的更多相关文章

  1. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  2. #leetcode刷题之路45-跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例:输入: [2,3,1,1,4]输出: 2 ...

  3. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  5. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  6. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  7. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  8. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  9. #leetcode刷题之路6- Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L     C     I ...

随机推荐

  1. 【codeforces 746E】Numbers Exchange

    [题目链接]:http://codeforces.com/problemset/problem/746/E [题意] 你有n张卡片,上面写着不同的数字; 然后另外一个人有m张上面写着不同的数字的卡片: ...

  2. 6.between...and...

    6.在WHERE中使用between...and...   用于区间值的条件判断(包含边界值)     //查询工资在2000(包含)到3000(包含)之间的员工信息   select empno,e ...

  3. linux mint(Ubuntu、Debian) 18修改环境变量

    修改环境变量 sudo gedit /etc/profile sudo gedit /etc/profile 在profile文件的末尾添加以下代码 export JAVA_HOME=/usr/lib ...

  4. 洛谷 P3133 [USACO16JAN]无线电联系Radio Contact

    P3133 [USACO16JAN]无线电联系Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the ...

  5. JDK+JDBC+MySQL实例及注意事项

    by qx.zhong Hangzhou 29 Jun 2014 开发环境 OS:  Win8.1 x64 JDK: 1.8 SE DB:  MySQL 5.5  Lib:  mysql-connec ...

  6. Android自己定义组件系列【4】——自己定义ViewGroup实现双側滑动

    在上一篇文章<Android自己定义组件系列[3]--自己定义ViewGroup实现側滑>中实现了仿Facebook和人人网的側滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布 ...

  7. Codeforces Round #311 (Div. 2)题解

    A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. python 3.x 学习笔记11 (静态、类、属性、特殊成员方法)

    1.静态方法通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法.静态方法是不可以访问实例变量或类变量的即没有self,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什 ...

  9. ZOJ 2883 Shopaholic【贪心】

    解题思路:给出n件物品,每买三件,折扣为这三件里面最便宜的那一件即将n件物品的价值按降序排序,依次选择a[3],a[6],a[9]----a[3*k] Shopaholic Time Limit: 2 ...

  10. python内置的一些模块

    logging模块: 默认情况下,logging将日志打印到屏幕,日志级别为WARNING:日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO & ...