题目

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. 【aspnetcore】模拟中间件处理请求的管道

    几个核心对象: ApplicationBuilder 就是startup->Configure方法的第一个参数,请求(HttpContext) 就是由这个类来处理的 HttpContext 这个 ...

  2. python学习之j进程和线程:

    每个进程至少有一个线程,python因为每个线程都共用一个GIL全局锁(同时只能运行一个线程),所以不能用多线程(除非重新写C解释器),但是多进程的GIL锁各自独立可多进程. 进程与线程的区别在于一个 ...

  3. 操作messageBox类

    我们经常操作messagebox类,有时候我们又分不清一些参数,下面是一些操作messageBox的常用方法: public static class ClsMsg { public static v ...

  4. Java 过滤器实现(登录) + 拦截器(两种方法)

    以下是实现未登录不能进入页面的实现 使用了thyemeleaf+SpringBoot+过滤器实现的,过滤器的核心代码如下: @Component @WebFilter(filterName = &qu ...

  5. SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解

    前台到后台的流程:前台jsp->后台:controller控制器层->service业务层->DAO数据访问层->数据库model模型层. 从上面流程可知,前台jsp的数据,想 ...

  6. JVM垃圾回收机制二

    对象的回收 垃圾的回收涉及的几个问题:何时回收,由谁回收,怎样回收.这几个问题我们一一来解决. 1.何时回收----对象的生死判定 对象达到什么条件才能判断这个对象已经无用了.常见的判断对象生死的方法 ...

  7. .aspx IIS发布404.17时候的问题

    .aspx IIS发布404.17时候的问题 在发布aspx的时候遇到问题利用网上的cmd加入注册表没有效果 在加入了MIME中加入.aspx和application/.aspx之后却变成了下载文件 ...

  8. 浅析document和window的区别

    1.执行时间 window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行.         $(document).ready()是DOM结构绘制完毕后就执行,不必等到加载完毕. 2 ...

  9. awk累加

    {a+=substr($14,1,1)}END{a=(a=="")?0:a;print a}' 对a进行累加,如果最后a=0的话,结果为0,否则为a,最后输出a

  10. Linux shell例子

    #!/bin/bash read -p "input a dight:"echo $REPLY DATE=`date`echo "DATE is ${DATE}" ...