【一天一道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 ...
随机推荐
- java随机生成字符串和校验
首先定义字符串 String a = "0123456789"; // 数字 String b = "abcdefghijklmnopqrstuvwxyz"; ...
- SQL语句删除字段,改变字段长度
1.改变字段长度 ALTER TABLE T_MSG_SEND_R_ACC MODIFY reply_content VARCHAR(512); 2.删除字段ALTER TABLE MSG_TX_BA ...
- linux上快速搭建宝塔面板来操作便捷功能
关于宝塔 Linux版请确保纯净系统安装(支持CentOS.Ubuntu.Debian.Fedora.Deepin),Web端管理,QQ群及论坛技术支持一键LAMP/LNMP.创建网站FTP.数据库. ...
- R语言中函数调试
有时候会用R语言写一下简单的脚本处理函数,加入需要调试的话可以按照下面的步骤进行: fun <- function(x , y){ x + y x - y x * y x / y } debug ...
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...
- jboss规则引擎KIE Drools 6.3.0 Final 教程(2)
使用JAVA程序调用规则-运行KIE-DROOLS上的规则 第一步:建立一个MAVEN的Java工程 POM.XML 给出pom.xml文件 <project xmlns="http: ...
- Objective-C构造方法
Objective-C构造方法 构造方法:用来初始化的方法 创建对象的原理 之前我们创建对象的方式一直是使用[Xxx new] 但是使用 new 创建的对象,都是给我们默认做了初始化的. 有的时候,我 ...
- Programming In Scala笔记-第四章、类和对象
类似于Java,Scala中也有类和对象的概念. 一.类.属性和方法 1.类 类是对一类事物的抽象,当一个类被定义后,就可以以该定义为模板,定义该类的一系列对象.比如说有以下一个模板 人类: 有姓名: ...
- Swift类中如何创建一个对外只读对内可读写的属性
很简单用private修饰符,后面跟限制关键字set: class Day{ private(set) var rawValue:Int = 0 func showRawValue(){ print( ...
- 使用java操作HDFS
新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...