题目

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,

分析

本题目与上一题LeetCode(112) Path Sum虽然类型相同,但是需要在以前的基础上,多做处理一些:

与Path Sum相比,本题是求出路径,所以,在找到满足的路径之后,不能直接返回,而是将其添加到一个vector < vector >中。在查找的过程中,每经过一个结点,先使用一个vector将该路径中的所有结点记录下来。由于vector< vector>是用来记录所有满足的路径的,所以传递引用给它是为了对它的每一个改动都是对其本身的操作,而不是对其副本的操作,使用来返回找到的所有路径。使用的vector只是拷贝操作,所以都是对其副本的操作,不会对原始的vector有影响。

AC代码

/**
* Definition for a binary tree node.
* 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) {
if (!root)
return vector<vector<int>>(); //存储所有满足条件的路径
vector<vector<int> > paths; //存储当前遍历路径
vector<int> tmp; getPaths(root, sum, paths, tmp); return paths; } void getPaths(TreeNode *root, int sum, vector<vector<int> > &paths, vector<int> tmp)
{
if (!root)
return; tmp.push_back(root->val);
if (!root->left && !root->right && sum == root->val)
paths.push_back(tmp); if (root->left)
getPaths(root->left, sum - root->val, paths, tmp); if (root->right)
getPaths(root->right, sum - root->val, paths, tmp);
} };

GitHub测试程序源码

LeetCode(113) Path Sum II的更多相关文章

  1. LeetCode(112) Path Sum

    题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  3. [LeetCode] #112 #113 #437 Path Sum Series

    首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...

  4. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  5. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  6. LeetCode(113):路径总和 II

    Medium! 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树,以及目标和 sum = ...

  7. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  8. LeetCode(307) Range Sum Query - Mutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  9. LeetCode(304)Range Sum Query 2D - Immutable

    题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

随机推荐

  1. matlab各向异性扩散滤波

    主要是用来平滑图像的,克服了高斯模糊的缺陷,各向异性扩散在平滑图像时是保留图像边缘的(和双边滤波很像). 通常我们有将图像看作矩阵的,看作图的,看作随机过程的,记得过去还有看作力场的. 这次新鲜,将图 ...

  2. Springboot2.X集成Quartz集群

    为什么要使用Quzrtz集群 在项目进行集群部署时,如果业务在执行中存在互斥关系,没有对定时任务进行统一管理,就会引起业务的多次执行,不能满足业务要求.这时就需要对任务进行管理,要保证一笔业务在所有的 ...

  3. pay-spring-boot 开箱即用的Java支付模块,整合支付宝支付、微信支付

    关于 使用本模块,可轻松实现支付宝支付.微信支付对接,从而专注于业务,无需关心第三方逻辑. 模块完全独立,无支付宝.微信SDK依赖. 基于Spring Boot. 依赖Redis. 我能做什么 支付宝 ...

  4. JAVA 时间的使用

    今天老师又没有讲新课,不过讲了练习题,扩展了一下我们的思维. 今天就讲一下如何获取时间吧. 代码: import java.util.* public class Test{ public stati ...

  5. vim编辑器高级应用

    1. vim主要模式介绍 命令模式.命令行模式.编辑模式 字符操作:i 当前插入, I行首插入, a当前字符之后插入,A行首插入, ESC退出当前模式 2. vim命令模式 3. vim插入模式 4. ...

  6. jQuery事件,对象以及插件

    回顾 1 基本使用 2 jquery 选择器 3 筛选器 过滤 查找 串联 4 DOM 操作 内部插入 append()appendTo()prepend()prependTo() 外部插入 afte ...

  7. 使用Azure CDN更快速的交付内容

    WEB加速服务是最基本也是应用最广泛的CDN加速服务,主要针对html文件,CSS,图片,JS,flash动画等更新频率低的小文件加速.通过将这些小文件缓存到Azure CDN的边缘节点,减少源站的访 ...

  8. Chrome浏览器扩展程序的本地备份

    由于众所周知的原因,有些朋友可能很难在线下载Chrome扩展程序.一种选择是可以让朋友把他成功安装的Chrome扩展程序导出成本地文件,然后让朋友发送给自己,在自己本地电脑上报这些本地文件直接拖到Ch ...

  9. Unity之脚本编译顺序

    根据官方的解释,它们的编译顺序如下: (1)所有在Standard Assets.Pro Standard Assets或者Plugins文件夹中的脚本会产生一个Assembly-CSharp-fil ...

  10. UVA1663 Purifying Machine (匈牙利算法,二分图最大匹配)

    模版集合个数减少是因为匹配串集合中没被匹配过的一对串匹配了.所以就是找一个二分图最大匹配. 因为集合X和Y是不好分开的,但是可以直接跑,两个集合都会跑一遍,所以一个匹配会被算两次,返回的时候除以2就行 ...