原题链接

子母题 112 Path Sum

跟112多了一点就是保存路径

依然用dfs,多了两个vector保存路径

Runtime: 16 ms, faster than 16.09% of C++

class Solution
{
public:
vector<vector<int>> res;
vector<int> temp;
vector<vector<int>> pathSum(TreeNode *root, int sum)
{
dfs(root,temp,,sum);
return res;
}
void dfs(TreeNode *root, vector<int> cur, int total, int sum)
{
if (root == NULL)
return;
total += root->val;
cur.push_back(root->val);
if (root->left == NULL && root->right == NULL)
{
if (total == sum)
{
res.push_back(cur);
}
return;
}
if (root->left)
dfs(root->left, cur, total, sum);
if (root->right)
dfs(root->right, cur, total, sum);
}
};

[leetcode] 113. Path Sum II (Medium)的更多相关文章

  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路径总和 II (C++)

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

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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. 浅议Delphi中的Windows API调用(举的两个例子分别是String和API,都不错,挺具有代表性)

    浅议Delphi中的Windows API调用http://tech.163.com/school • 2005-08-15 10:57:41 • 来源: 天极网为了能在Windows下快速开发应用程 ...

  2. ZooKeeper学习第一期---Zookeeper简单介绍(转)

    转载来源:https://www.cnblogs.com/sunddenly/p/4033574.html 一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术--分布式协调技 ...

  3. 分享android ADT百度云盘下载地址

    由于android官网经常无法打开,特意把最新的android ADT和SDK放到了百度云盘进行了分享,目录中包含Windows和Macbook两种平台的版本,请自行选择: http://pan.ba ...

  4. 无法启动print spooler服务,错误2,系统找不到指定的文件

    来自百度: 无法启动print spooler服务,错误2,系统找不到指定的文件 我的打印机无法运行:出现"打印后台程序没有执行"提示.查:print spooler没有启动.点击 ...

  5. J2SE的基本简介与J2EE/J2ME的差异

    J2SE简介与J2EE.J2ME的比较 Java2平台包括:标准版(J2SE).企业版(J2EE)和微缩版(J2ME)三个版本. J2SE,J2ME和J2EE,这也就是SunONE(Open NetE ...

  6. 如何让apache支持.htaccess 解决Internal Server Error The server …错误

    如何让apache支持.htaccess 解决Internal Server Error The server …错误 文章来源:小灰博客| 时间:2013-12-25 12:17:08| 作者:Le ...

  7. Effective Java - 静态方法与构造器

    目录 用静态工厂方法替代构造器? 静态工厂有名称 静态工厂不必重新创建一个对象 静态工厂可以返回任何子类型对象 静态工厂返回的类可以动态变化 静态工厂返回的类可以不存在 静态工厂方法的缺点 静态工厂方 ...

  8. 提升——树形DP

    这里讲提高一点的内容,所以没有树形DP基础的,先看一下基础部分: 浅说——树形DP 闲言不表,看第一题. 这道题是典型的树上最长链问题.(就是一个模板题) 给定一棵树,树上共有N个节点(N<=5 ...

  9. django基础知识之QueryDict对象:

    QueryDict对象 定义在django.http.QueryDict request对象的属性GET.POST都是QueryDict类型的对象 与python字典不同,QueryDict类型的对象 ...

  10. Markdown下,上传图片问题

    最简单的方法: 1,登录qq 2,登录博客园,并打开博客园添加随笔的地方:如图: 3,选择需要截屏的地方,按住ctrl+alt+A截屏,然后在qq的发送栏内贴过去 4,鼠标左键按住不松开,然后拖到这里 ...