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

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

示例:

给定如下二叉树,以及目标和 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. springcloud Eureka Finchley.RELEASE 版本

    创建一个父项目cloud-demo pom.xml <?xml version="1.0" encoding="UTF-8"?> <proje ...

  2. 2018-8-10-使用-ahk-让普通键盘变为Dvorak键盘

    title author date CreateTime categories 使用 ahk 让普通键盘变为Dvorak键盘 lindexi 2018-08-10 19:16:51 +0800 201 ...

  3. UMP系统架构 RabbitMQ

  4. BOM相关知识点

    1.BOM概念:Browser Object Model 浏览器对象模型作用:提供了使用JS操作浏览器的接口 2.BOM包含了许多对象信息,包括如下这些:(1)screen 屏幕信息(2)locati ...

  5. [JZOJ3168] 【GDOI2013模拟3】踢足球

    题目 描述 题目大意 有两个队伍,每个队伍各nnn人. 接到球的某个人会再下一刻随机地传给自己人.敌人和射门,射门有概率会中. 每次射门之后球权在对方111号选手. 某个队伍到了RRR分,或者总时间到 ...

  6. Nginx在windows系统的常用命令

    启动 start nginx 强制停止 nginx.exe -s stop 重启 nginx.exe -s reload

  7. pyQT Dialog默认选中某一个选项问题的解决

    方法一: 在新建ui文件时不要新建Dialog # -*- coding: utf-8 -*- # Form implementation generated from reading ui file ...

  8. UICollectionView入门--使用系统UICollectionViewFlowLayout布局类

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbownight.blog.51cto.com/1336585/13237 ...

  9. Charles抓包(http/https请求)

    Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装官网下载安装Charles:https://www.charlesproxy.com/download/当然由于国情可以使用破 ...

  10. python 读取excel Xlrd模块

    1. 安装xlrd模块 我使用pip安装: cmd ->切换到pip安装所在路径->pip install xlrd->回车 2. 使用 2.1:打开Excel表 导入模块:  im ...