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

For example:
Given the below binary tree and sum = 22,

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

return

[
[5,4,11,2],
[5,8,4,5]
]
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) {
pathGroup.clear();
if(!root) return pathGroup; target = sum;
vector<int> path;
preOrder(root,,path);
return pathGroup; }
void preOrder(TreeNode* node,int sum,vector<int> path){
sum = node->val + sum;
path.push_back(node->val);
if(node->left)
{
preOrder(node->left,sum,path);
} if(node->right)
{
preOrder(node->right,sum,path);
} if(!node->left && !node->right)
{
if(sum==target)
{
pathGroup.push_back(path);
}
}
}
private:
int target;
vector<vector<int>> pathGroup;
};

113. Path Sum II (Tree; DFS)的更多相关文章

  1. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  2. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

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

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

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. 【LeetCode】113. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  6. Path Sum II 总结DFS

    https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf p ...

  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. 113. Path Sum II

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

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

随机推荐

  1. 国外牛人的五个Kubernetes学习建议

    现在云中存在的许多系统都是建立在基于虚拟机,虚拟磁盘等物理概念的抽象基础之上的,”Heptio的联合创始人兼首席技术官兼Kubernetes在Google的原始开发人员之一的Joe Beda说. “K ...

  2. git 获取远程分支

    另一哥们将分支push到库中,我怎么获取到他的分支信息呢? 如果安装了git客户端,直接选择fetch一下,就可以获取到了. 如果用命令行,运行 git fetch,可以将远程分支信息获取到本地,再运 ...

  3. 常用ETL工具

    一 ETL工具[国外] 1. datastage点评:最专业的ETL工具,价格不菲,使用难度一般 下载地址:ftp://ftp.seu.edu.cn/Pub/Develop ... taStage.v ...

  4. JEECG获取当前登录人的值

    TSUser user = ResourceUtil.getSessionUserName(); mv.addObject("fbillerid", user.getUserNam ...

  5. 引用 LPSTR、LPCSTR、LPTSTR、LPCTSTR、LPWSTR及LPCWSTR的意义及区别

    1.ANSI(即MBCS):为多字节字符集,它是不定长表示世界文字的编码方式.ANSI表示英文字母时就和ASCII一样,但表示其他文字时就需要用多字节.   2.Unicode:用两个字节表示一个字符 ...

  6. (转)Inno Setup入门(二)——修改安装过程中的图片

    本文转载自:http://blog.csdn.net/augusdi/article/details/8564793 修改安装过程中的图片 一般编译之后,安装过程中出现在左边图片是是下图这个样子的: ...

  7. 将h264和aac码流合成flv文件

    在视频应用中,经常需要将接收到h264和aac数据保存成文件. 本来想用mp4格式,但是mp4在没有正常关闭的情况下会导致文件打不开,而在实际应用中经常会出现设备直接拔电,程序不是正常结束的情况.于是 ...

  8. Java 编译???

    如果是在命令行下,编译就是 javac a.java 如果有错误,那么命令运行之后会显示错误 但是在eclipse下,我都是直接点击运行按钮的,如果有错误,在编程是就提示了,那么是怎么编译的呀 大的工 ...

  9. "网页安全政策"(Content Security Policy,缩写 CSP)的来历

    作者:阿里聚安全链接:https://www.zhihu.com/question/21979782/answer/122682029来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  10. python删除安装的模块

    上篇讲述了如何用distutils模块来创建分发包,那么安装了模块之后,怎么来删除呢,具体的步骤如下: [root@FTP ansible]# ls -l /usr/share/kel -rw-r-- ...