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. STM32利用TIM3产生一个1--100Hz可调频率

    目标:利用TIM3结合普通GPIO实现一个1--100HZ的可控频率,误差在0.5HZ以内 核心:要实现该功能首先要明确频率的定义,频率就是1秒内发生周期性变化的次数,例如一个正弦波,1S内,走了15 ...

  2. MgdDbg工具

    ArxDbg是可以查看AutoCAD内部数据结构的工具,可惜是C++的.从网上找到了一个.NET版本的MgdDbg,实现的功能与C++版本的差不多. 1.运行程序,你只要右键点击AutoCAD窗口,在 ...

  3. libvips

    libvips : an image processing library libvips is a 2D image processing library. Compared tosimilar l ...

  4. Revolution Platform

    Revolution Platform 黑暗的极权统治现实 异类的处境 独孤的存在 觉者的形成 信仰的确立 信仰的产物 完整的思想理论 反抗与信仰的一致 反抗的超理性的智慧论 反抗的纯理性的方法论 反 ...

  5. ZOJ 3689 Digging(贪心+dp)

    Digging Time Limit: 2 Seconds      Memory Limit: 65536 KB When it comes to the Maya Civilization, we ...

  6. java中的system.out.println()和JSP中out.println()差别

    out.println()输出到client.     在out.println()中,out是response的实例.是以response为对象进行流输出的,即将内容输出到client.假设在JSP ...

  7. 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

    pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...

  8. ElasticSearch 深入理解 三:集群部署设计

    ElasticSearch 深入理解 三:集群部署设计 ElasticSearch从名字中也可以知道,它的Elastic跟Search是同等重要的,甚至以Elastic为主要导向. Elastic即可 ...

  9. 2.windows下安装git

    转自:https://blog.csdn.net/lvkelly/article/details/54666868

  10. luogu 1941 飞扬的小鸟

    这道题对于第13个数据点,不知为什么f数组第二位开到2000以下就不能过,求指教 飞扬的小鸟 传送门 题目大意 一个小鸟在\(n*m\)的方阵里,然后有许多管道你们玩过就不多介绍了,然后每一个位置,点 ...