/**
* 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>> res;
if(root == NULL) return res;
vector<int> add;
add.push_back(root->val);
DFS(res,add,root,sum,root->val);
return res; }
void DFS(vector<vector<int>>& res,vector<int>& add,TreeNode*root,int sum,int& he){
if((root->left == NULL)&&(root->right == NULL)){
if(he == sum) res.push_back(add);
}
else if((root->left != NULL)&&(root->right == NULL)){
add.push_back(root->left->val);
he += root->left->val;
DFS(res,add,root->left,sum,he);
add.pop_back();
he -= root->left->val;
}
else if((root->left == NULL)&&(root->right != NULL)){
add.push_back(root->right->val);
he += root->right->val;
DFS(res,add,root->right,sum,he);
add.pop_back();
he -= root->right->val;
}
else if((root->left != NULL)&&(root->right != NULL)){
add.push_back(root->left->val);
he += root->left->val;
DFS(res,add,root->left,sum,he);
add.pop_back();
he -= root->left->val;
add.push_back(root->right->val);
he += root->right->val;
DFS(res,add,root->right,sum,he);
add.pop_back();
he -= root->right->val;
}
return;
}
};

_虽然代码丑,但比较好理解

class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int>> res;
vector<int> out;
helper(root, sum, out, res);
return res;
}
void helper(TreeNode* node, int sum, vector<int>& out, vector<vector<int>>& res) {
if (!node) return;
out.push_back(node->val);
if (sum == node->val && !node->left && !node->right) {
res.push_back(out);
}
helper(node->left, sum - node->val, out, res);
helper(node->right, sum - node->val, out, res);
out.pop_back();
}
};

——这个和上一题对应

Leetcode 113的更多相关文章

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

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

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

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

  4. Java实现 LeetCode 113 路径总和 II

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

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

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

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

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

随机推荐

  1. Linux中Postfix邮件WebMail配置(七)

    Extmail Extmail 是一个以perl语言编写,面向大容量/ISP级应用,免费的高性能Webmail软件,主要包括ExtMail.Extman两个部分的程序套件.ExtMail套件用于提供从 ...

  2. python之路----面向对象进阶一

    一.isinstance和issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object): pass obj = Foo() i ...

  3. ORM 关系对象映射 基础知识点

    优点: 1.ORM使我们通用的数据库变得更加的简单便捷. 2.可避免新手程序员写sql语句带来的性能问题. 1. 创建单表 2. 创建关键表 1). 一对一 2). 一对多 3). 多对多 创建表的语 ...

  4. 20145227鄢曼君《网络对抗》MSF基础应用

    20145227鄢曼君<网络对抗>MSF基础应用 主动攻击:ms08_067漏洞攻击实践 两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版).在VMware中设置 ...

  5. 20145328《网络对抗技术》Final

    系内选拔赛write-up 1 信息隐藏 第一题图片藏东西,后缀名改txt,没有发现,改rar,发现压缩包内存在key.txt,解压提示存在密码,尝试使用修复,得到key.txt,打开获取flag,S ...

  6. C++ 文件大小格式化

    #include <iostream> #include <windows.h> using namespace std; /*文件大小格式化 *param [in] dwSi ...

  7. Delphi XE5 for Android (七)

    Delphi XE5下,TMainMenu和TPopupMenu不可用,那么如何支持Android的菜单呢? 查看了一些资料,不得要领,只是摸索着先模拟一下吧. 首先在窗体上放置一个TPanel,在其 ...

  8. 【angular2+typeScript+ng-zorro】Carousel 走马灯的左右方向控件实现

    ng-zorro Carousel 走马灯的左右方向控件实现 ng-zorro框架的走马灯本身还没有左右方向控件的实现,作者只是在文档中(0.6x)中曝出几个方法接口,如图: 实现: 在根compon ...

  9. C#下载歌词文件

    前段时间写了一篇c#解析Lrc歌词文件,对lrc文件进行解析,支持多个时间段合并.本文借下载歌词文件来探讨一下同步和异步方法. Lrc文件在网络上随处可见,我们可以通过一些方法获取,最简单的就是别人的 ...

  10. 06_Flume_interceptor_时间戳+Host

    1.目标场景 2.flume agent配置文件 # define agent name, source/sink/channel name a1.sources = r1 a1.sinks = k1 ...