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 ...
随机推荐
- EasyTrader踩坑之旅(三)
快速阅读 用THSTrader 调试同花顺自动下单的过程 . 主要原理是利用python函数pywinauto 自动获取同花顺上相应控件的值,进行模拟自动化的操作,不得不说python函数库的强大 ...
- Out of range value for column 'huid' at row
遇到一个MySQL小问题 Data truncation: Out of range value for column 'huid' at row 1 在数据库某表中字段 “huid” 为 ...
- CUDA编程之快速入门【转】
https://www.cnblogs.com/skyfsm/p/9673960.html CUDA(Compute Unified Device Architecture)的中文全称为计算统一设备架 ...
- Rare-Variant Association Analysis | 罕见变异的关联分析
Rare-Variant Association Analysis: Study Designs and Statistical Tests 10 Years of GWAS Discovery: B ...
- mybatis generatorConfig.xml生成配置文件及三种运行方式
https://blog.csdn.net/gavin5033/article/details/83002335 一 ,cmd命令执行配置文件本人工作目录结构(图一) 在自己放配置文件的目录下新建ge ...
- At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger fo
一.文章前言 本文是亲测有效解决At least one JAR was scanned for TLDs yet contained no TLDs问题,绝对不是为了积分随便粘贴复制然后压根都没有用 ...
- Java 泛型 (Generics)
泛型:就是变量类型的参数化 泛型是JDK1.5中的一个最重要的特征.通过引入泛型,我们将获得编译时类型的安全和运行时更小的抛出ClassCastException的可能. public class A ...
- Win10 剪贴板 快捷键是什么?
使用基于云的剪贴板从一台电脑上复制图像和文本并粘贴到另一台电脑上.你不仅可以从剪贴板历史记录中粘贴,还可以固定你发现自己经常使用的项目. 若要随时访问剪贴板历史记录,请按 Windows 徽标键 ...
- linux 查看gpu信息
- 使用OpenCV(C ++ / Python)进行人脸交换
-- 图3.面部对齐.左:检测到面部标志和凸包.中:凸包上的点的Delaunay三角剖分.右:通过仿射扭曲三角形进行面部对齐. 1 人脸对齐 1.1 脸部地标检测 两个脸部的几何形状非常不同,因此我们 ...