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 ...
随机推荐
- 【转载】使用Response.WriteFile输出文件以及图片
Response对象是Asp.Net应用程序中非常重要的一个内置对象,其作用为负责将服务器执行好的信息输出给客户端,可以使用Response.WriteFile方法来像客户端输出文件或者图片,输出图片 ...
- 对于vue中axios拦截器简单封装
axios.interceptors.response.use( response => { // 如果返回的状态码为200,说明接口请求成功,可以正常拿到数据 // 否则的话抛出错误 if ( ...
- VSCode 搭建 React Native 环境
安装 React Native Tools 在插件市场搜索 react 找到 React Native Tools 进行安装: 创建的react-native的工程拖入vscode中 点击F5即可运行 ...
- JSONObject和URL以及HttpURLConnection的使用
1 将java对象类转成json格式 首先引入依赖jar文件 注意依赖文件的版本号,高版本可能没有对应的类 2 我的实体类中包含内部类注意内部类要public才能被序列化成json格式 import ...
- Java 函数调用是传值还是传引用? 从字节码角度来看看!
原文地址:点击打开
- HBase hbase-site.xml 参数
该文档是用hbase默认配置文件生成的,文件源是 hbase-default.xml. 在实际的HBase生产环境中应用于%HBASE_HOME%/conf/hbase-site.xml中. hbas ...
- 系统API是原子操作吗?
系统API里面也会有简单的指令,类似于a++这种,我们认为的简单指令对应到汇编可能很多条.执行在其中某一条汇编的时候可能就切换进程了.切换进程可能发生在用户态(虚拟内存的用户空间),也可能发生在内核态 ...
- 详解Linux磁盘管理与文件系统
磁盘基础 硬盘结构 物理结构 盘片:硬盘有多个盘片,每盘片 2 面. 磁头:每面一个磁头. 数据结构 扇区:磁盘上的每个磁道被等分为若干个弧段,这些弧段便是硬盘的扇区. 硬盘的第一个扇区,叫做引导扇区 ...
- OAuth2在微服务架构中的应用
首先是为什么要在微服务场景使用OAuth2,这是因为使用了OAuth2后,就能向第三方系统提供授权. 其次是如何使用,见下图: 在微服务架构中使用OAuth2,有几个问题需要我们思考: 1. toke ...
- java基础(1)----简介
基础语法. 面向对象. 字符串和集合. IO流. 接口. lambda. 方法引用. Stream. 模块化. 一.java的前世今生: J2SE:标准体验版.J2EE:企业版.J2ME:小型版(移动 ...