Leetcode题 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
代码如下:
/**
* 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 findPath(TreeNode* t,vector<int>& temp, vector<vector<int> >& record)
{
temp.push_back(t->val);
if(t->left!=NULL) findPath(t->left, temp, record);
if(t->right!=NULL) findPath(t->right, temp, record);
//到达叶子节点,就记录路径
if(t->left==NULL && t->right==NULL)
{
record.push_back(temp);
temp.erase(temp.end()-1);
return ;
}
//中间节点返回
temp.erase(temp.end()-1);
return;
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<vector<int>> record;
vector<string> srecord;
if(root==NULL) return srecord;
vector<int> temp;
//找到所有的路
findPath(root,temp,record);
//把所有的路按照题目要求存储
string stemp;
for(int i=0;i<record.size();i++)
{
stringstream ss;
for(int j=0;j<record[i].size();j++)
{
ss<<record[i][j]<<"->";
}
stemp.clear();
stemp=ss.str();
//去掉最后的"->"
stemp.pop_back();
stemp.pop_back();
srecord.push_back(stemp);
}
return srecord;
}
};
Leetcode题 257. Binary Tree Paths的更多相关文章
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [刷题] 257 Binary Tree Paths
要求 给定一棵二叉树,返回所有表示从根节点到叶子节点路径的字符串 示例 ["1->2->5","1->3"] 思路 递归地返回左右子树到叶子节 ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- mongodb 备份与还原操作
环境信息: 版本: mongodb 3.2 备份: mongodump 恢复: mongorestore 本文参考官方文档:https://docs.mongodb.com/manual/refere ...
- iOS CALayer总结——图层几何
最近看了一下关于图层和动画的内容,所以写了一份总结,算是对这些内容的汇总吧,都是一些简单的基础知识,不知道大家都了不了解. 除了和用户的交互之外,图层的很多属性和视图基本上都是一样的,今天就先从CAL ...
- 【完整篇】orangepi香橙派新手入门之被官方坑
图片特意缩小,看不清请打开另一个窗口查看原图. 第一步:烧录系统,我烧录的是Ubuntu_Desktop[请注意!!!!用户名是错的!!用户名是错的!!用户名是错的!!] 正确的用户名是orangep ...
- 五、DML操作汇总
前言: DML(Data Manipulation Language)数据操作语言,以INSERT.UPDATE.DELETE三种指令为核心,分别代表插入.更新与删除,是必须要掌握的指令,DML和SQ ...
- CQRS的全称是Command Query Responsibility Segregation
CQRS时,我们在讨论些神马? 当我写下这个标题的时候,我就有些后悔了,题目有点大,不太好控制.但我还是打算尝试一下,通过这篇内容来说清楚CQRS模式,以及和这个模式关联的其它东西.希望我能说得清 ...
- P1363 幻象迷宫[搜索]
题目描述 (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊-- LHX:momo...我们一定能走出去的! WD:嗯,+U+U ...
- 优雅的处理vue注册全局组件
使用情景: 有频繁使用的组件 需要进行全局注册 可以将这些组件都放在components下同一个文件夹下,在此文件夹中建立 一个js文件 代码如下: import Vue from 'vue'; // ...
- Robot Framework--连接Mysql数据库
1.安装Database-Library 输入命令:pip install robotframework_databaselibrary 2.添加Database的Library 3.实例 *** T ...
- java中的volatile变量
同步与线程间通信: 通信 通信是指消息在两条线程之间传递. 既然要传递消息,那接收线程 和 发送线程之间必须要有个先后关系,此时就需要用到同步.通信和同步是相辅相成的. 同步 同步是指,控制多条线程之 ...
- Java实习生面试题分享
1.Java有那些基本数据类型,String是不是基本数据类型,他们有何区别. Java语言提供了八种基本类型: 六种数字类型(四个整数型,两个浮点型) 字节型byte 8位 短整型short 16位 ...