一天一道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. js遍历 for-of

    for-of遍历 entries() 返回一个遍历器对象,用来遍历[键名, 键值]组成的数组.对于数组,键名就是索引值:对于 Set,键名与键值相同.Map 结构的 Iterator 接口,默认就是调 ...

  2. return、break和continue

    return.break和continue 这三个关键字有一个共同点,那就是读能让后面的语句不执行,不同的地方就是挑的距离不一样. return很强大,如果一个函数中有一个return,并且执行了,那 ...

  3. static class 静态类(Java)

    一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...

  4. HTML入门知识

    B/S:浏览器-服务器 C/S:客户端-服务器 更新麻烦 管理麻烦 PHP:基于BS结构进行项目开发的语言 ====================HTML:超文本标记语言 -- 控制网面内容CSS: ...

  5. acm几何

    fzu 2231,N个点求构成的平行四边行个数. 题意简重点在优化上 #include <cstdio> #include <iostream> #include <cs ...

  6. Android需求之点击跳转至市场评价

    相信大家都看过APP上有一个选项"喜欢此APP?还希望您评价一下吧!",然后点击弹出选择框让你选择一个市场如: 安智市场,百度应用,豌豆荚-.然后选择其中一个后就跳转至此市场你的A ...

  7. SpringBatch前言

    批处理应用程序就是对程序进行批量处理. 特点:批量应用程序处理大量数据而无需人类干预. 用途:可以选择使用批处理程序来计算每月的财务报表.计算统计和索引文件的数据等等. 应用:当银行系统中有很多业务都 ...

  8. 基于AOP的iOS用户操作引导框架设计

    背景 有一种现象,App设计者觉得理所当然的操作方式,却常常被用户所忽视,为了防止这种现象发生,就要为App设计一个帮助,一种低成本的方案是将帮助文档写成HTML然后展示给用户,这样的方式常常不能带来 ...

  9. Linux下端口复用(SO_REUSEADDR与SO_REUSEPORT)

    freebsd与linux下bind系统调用小结:    只考虑AF_INET的情况(同一端口指ip地址与端口号都相同) freebsd支持SO_REUSEPORT和SO_REUSEADDR选项,而l ...

  10. Texlive 更新命令

    设置repository tlmgr repository set http://mirror.hust.edu.cn/CTAN/systems/texlive/tlnet 上面的例子使用的是华中科技 ...