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 ...
随机推荐
- QuickJS 快速入门 (QuickJS QuickStart)
1. QuickJS 快速入门 (QuickJS QuickStart) 1. QuickJS 快速入门 (QuickJS QuickStart) 1.1. 简介 1.2. 安装 1.3. 简单使用 ...
- ios9 字符串与UTF-8 互相转换
在数据网路请求或其他情况下,需要将字符串转换成UTF-8编码 ios9后对其方法进行了修改 NSString *str = @"北京"; 把这个转成UTF8以前我们使用的是 NSS ...
- Source Insight用法
快捷键 Symbol: Browse Project Symbols-:F7, Alt+G Relation View Relationship->For Functions->Pefer ...
- Thymeleaf 模板
Thymeleaf 模板布局 th:fragment.th:replace.th:insert.th:remove th:fragment 模板布局 模板片段说明 模板中,经常希望从其他模板中包含⼀ ...
- Elasticsearch7
elasticsearch 由来 点击进入 elasticsearch 基本概念 点击进入 elasticsearch 安装 点击进入 elasticsearch 增删改查 点击进入 elastics ...
- (坑爹错误)记录prometheus中配置alertmanager.yml一次报错
global: resolve_timeout: 5m #处理超时时间,默认为5min smtp_smarthost: 'smtp.sina.com:25' # 邮箱smtp服务器代理 smtp_fr ...
- Linux 命令之 ln
ln 的作用是制作一个文件或者目录的快捷方式,让我们在使用的过程当中更加方便地使用. 下面我来简单介绍一下 ln 的基本用法. ln 的基本语法 生成一个软链 ln -s source_name li ...
- 虚拟机更改MAC
有两种方式修改MAC地址 方法一 现实中网卡一出厂就有MAC地址,虚拟机的MAC地址见下图,这个就相当于出厂MAC.在这里修改MAC相当于直接修改硬件MAC 方法二 这里修改MAC,硬件MAC并没有变 ...
- Nginx突破高并发的性能优化 - 运维笔记
在日常的运维工作中,经常会用到nginx服务,也时常会碰到nginx因高并发导致的性能瓶颈问题.今天这里简单梳理下nginx性能优化的配置(仅仅依据本人的实战经验而述,如有不妥,敬请指出~) 一.这里 ...
- 关于Mock的一些网站
https://github.com/google/googletest/tree/master/googlemock https://blog.csdn.net/hhb200766/article/ ...