一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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]

]

(二)解题

题目大意:给定一个二叉树和一个整数,求所有根节点到叶子节点上的节点值的和等于该整数的路径。

相比于上一题,【一天一道LeetCode】#112. Path Sum,本题需要保存该路径上的所有节点的值。

解题思路还是一样,但是需要用一个vector来存储路径上的节点。

/**
 * 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) {
         vector<vector<int>> ret;//用来存放结果
         if(root==NULL) return ret;
         dfsTree(root,sum,0,ret,vector<int>(0));
         return ret;
    }
    //cur:当前路径上的节点值和
    //ret:符合条件的路径的集合
    //temp:当前路径上的节点集合
    void dfsTree(TreeNode* root, int& sum,int cur ,vector<vector<int>>& ret,vector<int> temp)
    {
        temp.push_back(root->val);
        cur += root->val;
        if(root->left==NULL&&root->right==NULL)//判断是叶子节点
        {
            if(cur == sum) ret.push_back(temp);//满足条件就压入集合
            return;
        }
        if(root->left!=NULL) dfsTree(root->left,sum,cur,ret,temp);//遍历左子树
        if(root->right!=NULL) dfsTree(root->right,sum,cur,ret,temp);//遍历右子树
    }
};

【一天一道LeetCode】#113. Path Sum II的更多相关文章

  1. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  2. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  3. [LeetCode] 113. Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. leetcode 113. Path Sum II (路径和) 解题思路和方法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  5. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  6. LeetCode 113. Path Sum II路径总和 II (C++)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  7. Leetcode 113. Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. leetcode 113 Path Sum II ----- java

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. [leetcode]113. Path Sum II路径和(返回路径)

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  10. Java for LeetCode 113 Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. Spring AOP @Around @Before @After 区别

    此段小代码演示了spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑.@After 是在所拦截方法执行之后执行一段逻辑.@A ...

  2. prop与attr的区别

    与prop一样attr也可以用来获取与设置元素的属性. 区别在于,对于自定义属性和选中属性的处理. 选中属性指的是 checked,selected 这2种属性 1. 对于自定义属性 attr能够获取 ...

  3. …… are only available on JDK 1.5 and higher 错误(spring 的jdk版本检测在jdk 8下的修订)

    正常方法 用更高的版本,因为spring向下兼容. 我公司需求的方法(真 迟早跑路) 1.在项目中创建一个package为org.springframework.core 2.在该package下面新 ...

  4. java处理数据库不支持的emoji

    一般数据库的编码是utf8,utf8是不支持存储表情符的,当存入的微信昵称带有表情符时就会出现乱码情况,有两种解决方法: 1.mysql数据库升级到5.5版本以上,utf8改为utf8mb4,utf8 ...

  5. Java transient关键字使用小结

      1.transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被transient关键字修饰的.2.被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被t ...

  6. JavaScript 题目(作用域)

    var length = 10 function fn(){ alert(this.length) } var obj = { length: 5, method: function(fn) { fn ...

  7. Linux的哲学思想

    1.一切皆文件:2.单一目的的小程序:3.组合小程序完成复杂任务:4.文本文件保存配置信息:5.尽量避免捕获用户接口:6.提供机制,而非策略. 说到底Linux的哲学思想在于方便和更好的管理后台,不同 ...

  8. JAVA中接口的使用

    抽象类是从多个类中抽象出来的模板,如果将这种抽象进行的更彻底,那么就是接口(interface)了.什么是接口,简单的讲,接口就是抽象类的进一步抽象,这种进一步的抽象只定义了一种规范,而不需要关心具体 ...

  9. ThreadLocal 遇上线程池的问题及解决办法

    ThreadLocal 称为线程本地存储,它为每一个使用它的线程提供一个其值(value)的副本.可以将 ThreadLocal<T> 理解成 Map<Thread, T>,即 ...

  10. Android源码解析——Toast

    简介 Toast是一种向用户快速展示少量信息的视图.当它显示时,它会浮在整个应用层的上面,并且不会获取到焦点.它的设计思想是能够向用户展示些信息,但又能尽量不显得唐突.本篇我们来研读一下Toast的源 ...