LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree
描述:
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Example:
You may serialize the following tree:
1
/ \
2 3
/ \
4 5
as "[1,2,3,null,null,4,5]"
Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
答案:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public: // Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(root == NULL){
return "#";
}
return to_string(root->val) + ","+serialize(root->left)+","+serialize(root->right);
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data == "#")return NULL;
stringstream s(data);
return helper(s);
} TreeNode* helper(stringstream& s){
string temp;
getline(s,temp,','); if(temp == "#"){
return NULL;
}else{
TreeNode* root = new TreeNode(stoi(temp));
root->left = helper(s);
root->right= helper(s); //stoi(str, 0, n); //将字符串 str 从 0 位置开始到末尾的 n 进制转换为十进制 return root;
}
} }; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
说明:
stringstream
是 C++ 提供的另一个字串型的串流(stream)物件,和 iostream、fstream 有类似的操作方式。要使用 stringstream, 必須先加入這一行:#include <sstream>。
第28行,使用 stringstream s(data) 将string类型的data转换为 stringstream类型的s,传递到helper函数里。问题是,既然我们要的是string,那么为什么不直接转换?其实目的是为了分割,原来的string是类似的格式 ”1,2,3,NULL,NULL,4“,所有的数字都是通过逗号隔开,如果要在c++中进行拆分,那么进行转化比较方便。
转化过后,使用 getline(s,temp,','); 。 以 逗号 为分界,将第一个逗号之前的内容,存入temp。
stoi
然后,你可以看到stoi函数。功能是:将 n 进制的字符串转化为十进制。
int a = stoi(str, , );
/*
0是从0位开始
2是2进制
默认是10进制,所以这道题目直接填入string 数字就好
return是int型
*/
再谈 string 和 char
在其它的地方曾看到 string 和 char 的对比,比较直观清晰,就贴在这里作为记录。
| 操作 | string | char列表 |
| 初始化 | string s; | char s[100]; |
|
获取第i个字节 |
s[i] | s[i] |
| 字符串长度 | s.length() or s.size() | strlen(s) |
| 读取一行 | getline(cin,s) | gets(s) |
| 设定某一行 | s = "KYK" | strcpy(s,"KYK") |
| 字符串相加 |
s = s+ "KYK"; s += "KYK" |
strcat(s,"KYK") |
| 字符串比较 | s == "KYK" | strcmp(s,"KYK") |
LC 297 Serialize and Deserialize Binary Tree的更多相关文章
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)
[抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 297. Serialize and Deserialize Binary Tree
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [leetcode]297. Serialize and Deserialize Binary Tree 序列化与反序列化二叉树
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 297. Serialize and Deserialize Binary Tree *HARD*
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- Leetcode 297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
随机推荐
- 深入理解JVM虚拟机10:JVM常用参数以及调优实践
转自http://www.rowkey.me/blog/2016/11/02/java-profile/?hmsr=toutiao.io&utm_medium=toutiao.io&u ...
- 三大框架 之 Spring(IOC控制反转、DI依赖注入)
目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...
- 【软工实践】团队Git现场编程实战
组长博客链接 博客链接 组员职责分工 队员 职责分工 恩泽 进行任务的划分与安排,调用API,负责餐饮商铺及商圈信息的获取 金海 解析API返回的json数据,提取有关信息 君曦 部分算法编写 季城 ...
- laravel中图片的删除
laravel中图片的删除 一.总结 一句话总结: laravel里面删除的话还是建议用Storage的delete方法,不建议用原生php的unlink方法,不然没找到文件可能会报异常 二.lara ...
- MySQL事务表和非事务表
查看 max_binlog_stmt_cache_size 参数解释时,有这么一句话 If nontransactional statements within a transaction requi ...
- SSH项目中使用struts-tags报错According to TLD or attribute directive in tag file, attribute test does not accept any expressions
在运行struts2标签页面时报错,代码如下:<%@ page language="java" pageEncoding="gbk"%><%@ ...
- springboot rabbitMQ 死信对列 实现消息的可靠消费
1 引入 maven 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- 使用java移位运算符进行转化
import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().sy ...
- sql 获取某一时段中每一天中最大的时间的一条记录
SELECT *FROM ( SELECT ROW_NUMBER() OVER( PARTITION BY CONVERT(CHAR(10), DataTime, 120) ORDER BY Data ...
- 转 Zabbix 3.2.6通过SNMP和iDRAC监控DELL服务器
https://www.cnblogs.com/saneri/p/7772641.html