LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14——257. Binary Tree Paths(easy)
目录
前言
题目
语言
思路
源码
后记
前言
数据结构感觉理论简单,实践起来很困难。
题目
给定一个二叉树,输出所有的根节点到叶节点的路径(以字符串的形式返回)
语言
C++
思路
学了数据结构,所以一看到这种输出路径的,就想到了先序遍历、中序遍历、后序遍历的递归方法。
首先写一个方法test,这个方法就是用来递归求路径的,形参有结点指针、字符串str和路径paths。首先判断结点指针是否为空,不为空则将这个结点的value加到字符串str后面,当然,他给的是int型的value,所以得通过to_string转化成字符串型,这样才能不断的加在字符串str的尾巴后面。接着分3种情况:
如果结点的左孩子右孩子指针均为空,那么说明他没有孩子,则直接将str加在vector的尾部
如果左孩子指针不为空,那么就递归调用test(root->left,str+"->",paths)
如果右孩子指针不为空,那么就递归调用test(root->right,str+"->",paths)
最后在他所给的方法中定义个vector<string>paths,然后调用方法test(root,"",paths),就能返回paths。
源码
/*** 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:void test(TreeNode* root,string str,vector<string> &paths){if(root!=NULL) {str=str+to_string(root->val);if(root->left==NULL&&root->right==NULL)paths.push_back(str);if(root->left!=NULL)test(root->left,str+"->",paths);if(root->right!=NULL)test(root->right,str+"->",paths);}}vector<string> binaryTreePaths(TreeNode* root) {vector<string>paths;test(root,"",paths);return paths;}
};
后记
做这题我先去查了C++int型如何转为string,然后就知道了to_string;接着考虑到没有孩子的情况,所以又去查了如何在尾部加上数据,所以知道了push_back这个方法。
LeetCode刷题记录14——257. Binary Tree Paths(easy)相关推荐
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
- LeetCode刷题记录7——824. Goat Latin(easy)
LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...
- LeetCode刷题记录5——441. Arranging Coins(easy)
LeetCode刷题记录5--441. Arranging Coins(easy) 目录 LeetCode刷题记录5--441. Arranging Coins(easy) 题目 语言 思路 后记 题 ...
- LeetCode刷题记录2——217. Contains Duplicate(easy)
LeetCode刷题记录2--217. Contains Duplicate(easy) 目录 LeetCode刷题记录2--217. Contains Duplicate(easy) 题目 语言 思 ...
- LeetCode刷题记录6——696. Count Binary Substrings(easy)
LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...
- LeetCode刷题记录4——67. Add Binary(easy)
LeetCode刷题记录4--67. Add Binary(easy) 目录 LeetCode刷题记录4--67. Add Binary(easy) 题目 语言 思路 后记 题目 今天这题是与字符串相 ...
- 算法记录 牛客网 leetcode刷题记录
算法记录 & 牛客网 & leetcode刷题记录 解题思路 STL容器 常用算法模板 堆排序 插入排序 快速排序 BFS层序遍历 二叉树 JZ55 二叉树的深度 BST(binary ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
最新文章
- 如何在MFC中读写配置文件
- 008 selenium html报告
- 轻松理解python中的_和__区别和含义
- windows 从端口找应用
- 编译原理(五)自底向上分析之算符优先分析法
- 现代软件工程 - 代码量等于树叶量
- golang 字符串操作实例
- opengl启动过程
- log4j配置时的位置问题
- 持续集成:软件质量改进和风险降低之道
- 软件开发的需求文档如何去写
- CVAL,PVAL,SVAL宏定义
- 【最新】白piao迅雷的下载速度(迅雷11亲测好用)
- 苹果电脑装Windows7系统U盘启动盘制作教程
- 解决thinkpad sl410 evc windows7硬盘安装ubuntu12.04以后,windows 无线出现断开网速慢等问题
- linux qos 软件,linux下QOS:应用篇
- Corona SDK 游戏开发引擎介绍
- c语言食堂消费管理系统,食堂消费管理系统_食堂财务管理系统v1.0单机版
- 七牛云——对象存储管理工具介绍
- php验证码刷新_PHP验证码刷新不了,是什么问题?
热门文章
- ExampleUnitTest的用法
- ubuntu之路——day8.1 深度学习优化算法之mini-batch梯度下降法
- zabbix 3.0 完全安装全解!
- spring Batch实现数据库大数据量读写
- 转 小辉_Ray CORS(跨域资源共享)
- 用js 判断datagrid 中的 checkbox 是否被选中
- ORB_SLAM2中的Sim3变换
- linux如何查看内存最大的几个文件,详解Linux如何查看当前占用CPU或内存最多的几个进程...
- uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)
- usaco Combination Lock