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]
]
Hide Tags

Tree Depth-first Search

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<vector<int> > paths;
public:
void dfs(TreeNode *node,int sum,int csum,vector<int> onePath){ //a能为引用
if(node==NULL)
return;
if(node->left==NULL && node->right==NULL){
if(node->val+csum==sum){
onePath.push_back(node->val);
paths.push_back(onePath);
}
return;
}
onePath.push_back(node->val);
dfs(node->left,sum,csum+node->val,onePath);
dfs(node->right,sum,csum+node->val,onePath);
} vector<vector<int> > pathSum(TreeNode *root, int sum) {
paths.clear();
vector<int> onePath;
dfs(root,sum,,onePath);
return paths;
}
};

Path Sum II深度优先找路径的更多相关文章

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

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

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

  3. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  4. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

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

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

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [leetcode]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 ...

  8. 【leetcode】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 ...

  9. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

随机推荐

  1. /etc/vimrc配置

    [root@guolicheng ~]# cat /etc/vimrc if v:lang =~ "utf8$" || v:lang =~ "UTF-8$" s ...

  2. 阿里云数据库再获学术顶会认可,一文全览VLDB最新亮点

    一年一度的数据库领域顶级会议VLDB 2019于当地时间8月26日-8月30日在洛杉矶圆满落幕.在本届大会上,阿里云数据库产品团队浓墨登场,不仅有多篇论文入选Research Track和Indust ...

  3. Interface Builder: What are the UIView's Layout iOS 6/7 Deltas for?

    up vote57down votefavorite 19 I just noticed the iOS 6/7 Delta property found under the UIView's str ...

  4. 软件-SecureCRT:SecureCRT

    ylbtech-软件-SecureCRT:SecureCRT SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件 ...

  5. jeecms 前台拦截器的研究与改造

    jeecms 前台拦截器的研究与改造 2013年12月24日 15:23:35 xinfei0803 阅读数 3511   jeecms出发点是面向大众的,具有前台开发性,也就是说,即时是未登录(游客 ...

  6. Angular-cli 的安装

    一.先安装node.js(若已经安装就跳过) 1.下载node.js安装包 http://nodejs.cn 按照自己电脑的配置,下载相应的安装包(我下载的是 node-v6.10.3-x64.msi ...

  7. jdk 数组位移运算

    1.采用先shift=31-Integer.numberOfLeadingZeros(scale);取int前面的补零个数31再减去拿到占得内存位长度 2.i偏移shift(其实等于I*位数) 加上b ...

  8. iOS CALayer之CAEmitterLayer粒子发射器的神奇效果

    https://www.jianshu.com/p/c54ffd7412e7 想必以前QQ空间的点赞效果大家都知道吧,点赞之后按钮周围会有一圈爆裂的小圆点:还有微信的红包雨表情动画等,以及烟花,火焰效 ...

  9. tensorflow中张量的理解

    自己通过网上查询的有关张量的解释,稍作整理. TensorFlow用张量这种数据结构来表示所有的数据.你可以把一个张量想象成一个n维的数组或列表.一个张量有一个静态类型和动态类型的维数.张量可以在图中 ...

  10. 查看linux系统的文件inode号码使用情况

    :~$ df -i 文件系统 Inode 已用(I) 可用(I) 已用(I)% 挂载点 udev % /dev tmpfs % /run /dev/sda2 % / tmpfs % /dev/shm ...