/**
* 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. 如何在Apache中运行Python脚本

    第一步:进入C:\Apache24\cgi-bin: 第二步:在cgi-bin目录下新建一个hello.py文件: 第三步: #!python #coding:utf-8 print("co ...

  2. C++系统时间及字符串转换参考资料

    https://msdn.microsoft.com/en-us/library/a442x3ye.aspx https://msdn.microsoft.com/en-us/library/fe06 ...

  3. win10锁屏界面无法更新

    win10的锁屏界面都是巨硬公司推送过来的,质量还不错,最近锁屏界面无法更新,解决方案如下: 以管理员身份运行cmd,分别运行如下两个命令 del /f /s /q /a "%userpro ...

  4. c++标准库多线程入门

    从c++ 11开始,语言核心和标准库开始引入了对多线程的原生支持.如下所示: int doSth(char c) { default_random_engine dre(c); uniform_int ...

  5. vc++之stdafx.h

    关于stdafx.h的解释,其实蛮多的,在vs中,既然创建c++工程的时候,默认会给生成main.cpp,并且自动包含了stdafx.h,而且stdafx.h不是c++标准的一部分,那么个人认为,理解 ...

  6. 最大子段和SP1716GSS3 线段树

    前言 spoj需要FQ注册,比较麻烦,大家就在luogu评测吧 题目大意: $n$ 个数,$q$ 次操作 操作$0 _ x_ y$把$A_x$ 修改为$y$ 操作$1 _ l _r$询问区间$[l, ...

  7. 分布式系统一致性算法Raft

    Raft 算法也是一种少数服从多数的算法,在任何时候一个服务器可以扮演以下角色之一:Leader:负责 Client 交互 和 log 复制,同一时刻系统中最多存在一个Follower:被动响应请求 ...

  8. spark-shuffle分析

    前言 shuffle是分布式计算系统中最重要的一部分,spark和mapreduce的shuffle的大体思路类似,在实现上有一些区分.Spark提供了插件式的接口,使用者可以通过继承ShuffleM ...

  9. 【eclipse】删除工作空间

  10. linux中批量替换文本中字符串--转载

    (一)通过vi编辑器来替换.vi/vim 中可以使用 :s 命令来替换字符串.:s/well/good/ 替换当前行第一个 well 为 good:s/well/good/g 替换当前行所有 well ...