给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

给定如下二叉树,以及目标和 sum = 22,

5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1

返回:

[ [5,4,11,2], [5,8,4,5] ]

class Solution {

public:

vector<vector<int> > res;

vector<vector<int> > pathSum(TreeNode* root, int sum)

{

if(root == NULL)

{

return res;

}

vector<int> v;

DFS(root, v, sum);

return res;

}

void DFS(TreeNode* root, vector<int> &v, int sum)

{

if(root == NULL)

return;

if(root ->left == NULL && root ->right == NULL)

{

if(sum == root ->val)

{

v.push_back(root ->val);

res.push_back(v);

v.pop_back();

}

return;

}

v.push_back(root ->val);

if(root ->left)

DFS(root ->left, v, sum - root ->val);

if(root ->right)

DFS(root ->right, v, sum - root ->val);

v.pop_back();

}

};

Leetcode113. Path Sum II路径总和2的更多相关文章

  1. 113 Path Sum II 路径总和 II

    给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22,              5             / \            4 ...

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

  3. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

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

  5. 第34-2题:LeetCode113. Path Sum II

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

  6. [Leetcode] 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 (路径和) 解题思路和方法

    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. LeetCode113 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. PagedListCore的使用

    关于在Core2.0中PagedListCore实现分页 一.引言 开发中在对大量数据展示的时候,出于对性能的考虑,我们往往是使用分页功能(用户需要那一页我们就返回给他那一页,而不是一次性加载所有的数 ...

  2. scrpy--分布式爬虫

    原来的scrapy中的Scheduler维护的是当前机器中的任务队列(存放着Request对象以及回调函数等信息) + 当前的去重队列(存放访问过的url地址) 实现分布式的关键就是需要找一台专门的主 ...

  3. 注解@Qualifier@Primary

    在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢 根据注入资源的注解不同实现的方式有一点小小的区别 在Controller中使用 @Auto ...

  4. Linux 下 Nand Flash 调用关系

    Nand Flash 设备添加时数据结构包含关系 struct mtd_partition        partition_info[] --> struct s3c2410_nand_set ...

  5. jmeter做bbs作业时提示404错误

    在用jemter做bbs作业时候,注册成功后会跳转到主页,在写主页的脚本的时候,将fiddler抓到的url复制到路径下方“/bbs/forum.php”,但是第一次复制的时候在/bbs/forum. ...

  6. 廖雪峰Java15JDBC编程-1关系数据库基础-1关系数据库简介

    1.数据库 1.1 定义 数据库是按照数据结构来组合.存储和管理数据的软件. 1.2 数据库模型 数据库有层次模型.网状模型.关系模型三种模型. 2 关系数据库 关系数据库是建立在关系模型上的数据库, ...

  7. SaaS加速器,到底加速了谁? 剖析阿里云的SaaS战略:企业和ISV不可错过的好文

    过去二十年,中国诞生了大批To C的高市值互联网巨头,2C的领域高速发展,而2B领域一直不温不火.近两年来,在C端流量饱和,B端数字化转型来临的背景下,中国越来越多的科技公司已经慢慢将触角延伸到了B端 ...

  8. 染色(dye)

    染色(dye) Description Serene 和 Achen 在玩染色游戏.Serene 和 Achen 站在一个 n 个点 m 条边的无向连通图中,在第 i 次玩染色游戏时,Serene 在 ...

  9. CCPC-Wannafly Summer Camp 2019 全记录

    // 7.19-7.29 东北大学秦皇岛校区十天训练营,题目都挂在了Vjudge上.训练期间比较忙,没空更博总结,回来继续补题消化. Day1 这天授课主题是简单图论,节奏挺好,wls两小时理完图论里 ...

  10. excel中将时间戳转换为日期格式

    将时间戳信息通常为s,将其转换的公式为: =TEXT((A1+8*3600)/86400+70*365+19,"yyyy-mm-dd hh:mm:ss")