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 ...
随机推荐
- JPA 原生SQ查询
参考文章 https://blog.csdn.net/coding_1994/article/details/84575943 https://blog.csdn.net/m0_37776094/ar ...
- Audit(二)--清理Audit数据
(一) 概述 Audit的数据主要存储在sys.aud$表中,该表默认位于system表空间中,我们根据需求,将该表移到了sysaux表空间中.由于审计数据量较大,需要经常关注sysaux表空间的使用 ...
- SetThreadAffinityMask windows下绑定线程(进程)到指定的CPU核心
原帖地址:https://www.cnblogs.com/lvdongjie/p/4476766.html 一个程序指定到单独一个CPU上运行会比不指定CPU运行时快.这中间主要有两个原因:1)CPU ...
- Intellij IDEA的Facets和Artifacts
Facets: Facets表述了在Module中使用的各种各样的框架.技术和语言.这些Facets让Intellij IDEA知道怎么对待module内容,并保证与相应的框架和语言保持一致. 使用F ...
- Python中if __name__ == '__main__':的作用和原理
if __name__ == '__main__':的作用 一个python文件通常有两种使用方法,第一是作为脚本直接执行,第二是 import 到其他的 python 脚本中被调用(模块重用)执行. ...
- Flutter -------- 加载本地图片资源和网络图片
在Flutter加载本地图片资源 在Flutter项目目录下创建文件夹 images ,在文件夹中添加几张图片 指定资源 pubspec.yaml文件中 version: 1.0.0+1 enviro ...
- 手动卸载wine-stable卸载
装了wine发现运行TIM全都乱码,于是手动卸载wine: 删除文件:(参考https://blog.csdn.net/taizhoufox/article/details/5496568) ~/.w ...
- spark ml pipeline构建机器学习任务
一.关于spark ml pipeline与机器学习一个典型的机器学习构建包含若干个过程 1.源数据ETL 2.数据预处理 3.特征选取 4.模型训练与验证 以上四个步骤可以抽象为一个包括多个步骤的流 ...
- error C1002: 在第 2 遍中编译器的堆空间不足
error C1002: 在第 2 遍中编译器的堆空间不足 fatal error C1083: Not enough space 打开VS2015 x64 x86 兼容工具命令提示符,在此命令行中再 ...
- Glide升级到4.x版本遇到的问题
Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency ...