【一天一道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 1
return
[
[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 ...
随机推荐
- PySC2是DeepMind的“星际争霸II学习环境”(SC2LE)的Python组件
PySC2是DeepMind的"星际争霸II学习环境"(SC2LE)的Python组件. 它暴露了暴雪娱乐公司的星际争霸II机器学习API作为Python RL环境. 这是Deep ...
- JVM指令集介绍
转载自:http://glutinit.iteye.com/blog/1263446 延伸参考 JVM接收参数和方法调用 void spin() { int i; for (i = 0 ...
- 获取X天后的日期
import java.util.Calendar; import java.util.Date; public class main { public static void main(String ...
- RxSwift 系列(八) -- Error Handing Operators
前言 本篇文章我们将学习RxSwift中的错误处理,包括: catchErrorJustReturn catchError retry retry(_:) catchErrorJustReturn 遇 ...
- Servlet技术 Cookie与Session
会话过程:用户打开浏览器,点击链接访问资源,最后到关闭浏览器的整个过程称之为会话. 会话使用:与服务器进行会话的过程中产生数据,数据被保存下来,服务器根据数据对客户进行辨别,做出个性化的响应. 介绍两 ...
- JVM内存模型及分区
Java虚拟机在程序执行过程会把jvm的内存分为若干个不同的数据区域来管理,这些区域有自己的用途,以及创建和销毁时间. JVM内存模型如下图所示: jvm管理的内存区域包括以下几个区域: 栈区: 栈 ...
- jQuery 选择器 prop() 和attr()
Day30 jQuery 1.1.1.1 什么是jQuery? n jQuery是javaScript的前端框架.对常见的对象和常用的方法进行封装,使用更方便. 它兼容CSS3,还兼容各种浏览器.文档 ...
- Oracle中查询和删除相同记录的3种方法
--创建测试表 )); ,'); ,'); ,'); ,'); ,'); ,'); commit; select * from test; --查询相同记录 ); select id,name fro ...
- 初始化mysql数据库——Activiti BPM
package com.initialize; import org.activiti.engine.ProcessEngine; import org.activiti.engine.Process ...
- Vasya the Hipster
One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red so ...