【一天一道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 ...
随机推荐
- js添加key为数字的对象,通过类似于通过访问数组的中括号形式访问对象属性
var obj={};obj[1] = "mm";obj[2]="nn";console.log(obj[1]); 同var obj={};obj[" ...
- JAVA的Date类与Calendar类(常用方法)
http://blog.csdn.net/xiaopihai86/article/details/50827945 1.用Java.util.Calender来实现 Calendar cal ...
- setTimeout、setInterval被遗忘的第三个参数
一.最近在看promise,惊奇的发现:原来 setTimeout不只有两个参数,我还能说什么呢?赶紧探探究竟. function multiply(input) { return new Promi ...
- Java线程池使用和常用参数
多线程问题: 1.java中为什么要使用多线程使用多线程,可以把一些大任务分解成多个小任务来执行,多个小任务之间互不影像,同时进行,这样,充分利用了cpu资源. 2.java中简单的实现多线程的方式 ...
- 2-学习GPRS_Air202(Air202开发板介绍和下载第一个程序)
http://www.cnblogs.com/yangfengwu/p/8887933.html 资料链接 链接:https://pan.baidu.com/s/1968t2QITuxoyXlE_Nz ...
- VMWare 虚拟机 共享文件夹
1.不能拷贝和直接拖拽文件至虚拟机系统中 解决办法: 通过共享文件夹的方式进行文件共享. (Win7 32位 10.0版本的虚拟机). ①:选择虚拟机 虚拟机 → 设置 如下图: ②: ...
- python笔记一(语言简介、解释器、输入输出)
一.python语言简介 一顿狂吹python目前有多火.多NB,哈哈哈,不过用起来心情确实很舒畅. 解释性语言:缺点,运行速度慢. 二.python解释器 与C.C++.java不同,以上都需要先将 ...
- springMVC源码解析--ViewResolverComposite视图解析器集合(二)
上一篇博客springMVC源码分析--ViewResolver视图解析器(一)中我们介绍了一些springMVC提供的很多视图解析器ViewResolver,在开发的一套springMVC系统中是可 ...
- linux下数据同步、回写机制分析
一.前言在linux2.6.32之前,linux下数据同步是基于pdflush线程机制来实现的,在linux2.6.32以上的版本,内核彻底删掉了pdflush机制,改为了基于per-bdi线程来实现 ...
- Java中获取文件大小的正确方法
本文出处:http://blog.csdn.net/djy1992/article/details/51146837,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在原始 ...