【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 1return
[
[5,4,11,2],
[5,8,4,5]
]
(二)解题
题目大意:给定一个二叉树和一个整数,求所有根节点到叶子节点上的节点值的和等于该整数的路径。
相比于上一题,【一天一道LeetCode】#112. Path Sum,本题需要保存该路径上的所有节点的值。
解题思路还是一样,但是需要用一个vector来存储路径上的节点。
/**
* 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>> ret;//用来存放结果
if(root==NULL) return ret;
dfsTree(root,sum,0,ret,vector<int>(0));
return ret;
}
//cur:当前路径上的节点值和
//ret:符合条件的路径的集合
//temp:当前路径上的节点集合
void dfsTree(TreeNode* root, int& sum,int cur ,vector<vector<int>>& ret,vector<int> temp)
{
temp.push_back(root->val);
cur += root->val;
if(root->left==NULL&&root->right==NULL)//判断是叶子节点
{
if(cur == sum) ret.push_back(temp);//满足条件就压入集合
return;
}
if(root->left!=NULL) dfsTree(root->left,sum,cur,ret,temp);//遍历左子树
if(root->right!=NULL) dfsTree(root->right,sum,cur,ret,temp);//遍历右子树
}
};
【一天一道LeetCode】#113. Path Sum II的更多相关文章
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [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 ...
- [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 ...
- 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 ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- spaCy is a library for advanced natural language processing in Python and Cython:spaCy 工业级自然语言处理工具
spaCy is a library for advanced natural language processing in Python and Cython. spaCy is built on ...
- TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人
简介 TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人. 文章包括一下几个部分: 1.为什么要尝试做这个项目? 2.为 ...
- DELL、HP、IBM X86服务器命名规则
DELL.HP.IBM X86服务器命名规则 各大服务器厂家对于自己的服务器命名都有一定的规则,通常会根据服务器的外观(如塔式.机架式.刀片等).处理器(如Intel或者AMD等).架构等信息来命名. ...
- bootstrap插件fileinput.js 出现出现$("#xxxx").fileinput({}); 不生效的情况解决
如果出现$("#xxxx").fileinput({}); 不生效的情况请将fileinput.js中最后几行注释掉: /* $(document).ready(function ...
- Linux下常用的配置
本文主要给出的都是一些常用的Linux配置,系统版本是基于CentOs6.3,供自己复习和新人学习,不当之处还请指正. vmware tools安装 虚拟机--->安装vmware tools ...
- 使用FFMPEG在windows平台下推rtmp流
使用FFMPEG在windows平台下推rtmp流 工作中习惯在Linux下面使用FFmpeg模拟推rtmp流,无奈家中的电脑都是windows系统,需要利用家中的带宽来测试流媒体服务器的性能.所以研 ...
- NavigationView使用过程的问题解决
NavigationView是android support design库提供的侧滑面板控件,通常与support v4库里的DrawerLayout侧滑控件搭配使用.以下是使用过程中遇到的问题及解 ...
- Linux下端口复用(SO_REUSEADDR与SO_REUSEPORT)
freebsd与linux下bind系统调用小结: 只考虑AF_INET的情况(同一端口指ip地址与端口号都相同) freebsd支持SO_REUSEPORT和SO_REUSEADDR选项,而l ...
- CSS3 滤镜学习
html篇 样式篇 grayscale sepia saturate hue-rotate invert opactiy brightness contrast blur drop-shadow 综合 ...
- Android下实现手机验证码
Android实现验证码 效果图 Github地址 地址:https://github.com/kongqw/Android-CheckView 使用 <kong.qingwei.demo.kq ...