LeetCode OJ: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.
For example, you may serialize the following tree
1
/ \
2 3
/ \
4 5
as "[1,2,3,null,null,4,5]"
, just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
这题实际上思路还是比较清晰的,遇见NULL的话那么存入一个#既可以了,解序列化的时候直接相应的处理#就可以了,代码如下:
/**
* 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) {
queue<TreeNode *> q;
string str = "";
if(!root) return str;
stringstream ss;
ss << root->val;
str += ss.str();
q.push(root);
while(!q.empty()){
TreeNode * t = q.front();
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
if(t->left){
ss.str("");
ss << "," << t->left->val;
str += ss.str();
}else{
ss.str("");
ss << "," << "#";
str += ss.str();
} if(t->right){
ss.str("");
ss << "," << t->right->val;
str += ss.str();
}else{
ss.str("");
ss << "," << "#";
str += ss.str();
}
q.pop();
}
//删除末尾的#字符
for(int i = str.length() - ; ; ){
if(str[i] == '#'){
str = str.substr(, i - );
i = str.length() - ;
}else{
break;
}
}
return str;
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(!data.size()) return NULL;
vector<string> dataVec;
int sz = data.length();
for(int i = ; i < sz;){//分割字符串,保存到vector中
if(data[i] != ','){
int j = i;
for(; j < sz && data[j] != ','; ++j)
;
string tmp = data.substr(i, j - i);//注意substr的使用细节
dataVec.push_back(tmp);
i = j + ; //跳过','符号
}
}
sz = dataVec.size();
TreeNode * root = new TreeNode(atoi(dataVec[].c_str()));
queue<TreeNode *>q;
q.push(root);
int i = ;
while(!q.empty() && i < sz){
TreeNode * t = q.front();
if(dataVec[i] != "#"){
t->left = new TreeNode(atoi(dataVec[i].c_str()));
q.push(t->left);
}else{
t->left = NULL;
}
i++;
if(i >= sz) break;
if(dataVec[i] != "#"){
t->right = new TreeNode(atoi(dataVec[i].c_str()));
q.push(t->right);
}else{
t->right = NULL;
}
q.pop();
i++;
}
return root;
}
};
吐槽一下,c++连个split函数都没有,写起来真是恶心,还要自己去分割字符串。
LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)的更多相关文章
- [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 ...
- Leetcode 297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...
- [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 ...
- [leetcode]297. Serialize and Deserialize Binary Tree一般二叉树的编解码
由于一般的前序遍历不能唯一的还原出原本你的二叉树,所以要改变一下: 记录二叉树的结构信息,也就是空节点用符号表示 一般的前序遍历只是记录了节点的前后顺序,通过记录空节点,每一层的结构就可以记录下来 解 ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- LeetCode 428. Serialize and Deserialize N-ary Tree
原题链接在这里:https://leetcode.com/problems/serialize-and-deserialize-n-ary-tree/ 题目: Serialization is the ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- 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 ...
随机推荐
- springboot springmvc 支持 https
Spring Mvc和Spring Boot配置Tomcat支持Https 背景 最近在项目开发中需要让自己的后端Restful接口支持https,在参考了很多前辈们的博客后总结了一些. Spring ...
- PyQt4 调用串口API pySerial API说明
pySerial API官方介绍链接 http://pyserial.readthedocs.io/en/latest/pyserial_api.html
- 剑指offer 面试5题
面试5题: 题目:请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 方法一: # -*- co ...
- Eclipse Find/Replace
1.Eclipse内容助手 选中Regular expressions,使用正则表达式进行匹配.图中出现了小黄灯,Ctrl+Space显示出帮助信息. 2.Wrap search(循环检索)选中后,检 ...
- json教程系列(2)-生成JSONObject的方法
生成JSONObject一般有两种方式,通过javabean或者map类型来生成.如下面的例子: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...
- OpenGL学习进程(5)第三课:视口与裁剪区域
本节是OpenGL学习的第三个课时,下面介绍如何运用显示窗体的视口和裁剪区域: (1)知识点引入: 1)问题现象: 当在窗体中绘制图形后,拉伸窗体图形形状会发生变化: #include ...
- ES6 随记(3.1)-- 字符串的拓展
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 4. 拓展 a. 字符串的拓展 有些字符需要 4 个字节储存,比如 \uD83D\uDE80 ...
- PCIE phy和控制器
转:https://wenku.baidu.com/view/a13bc1c20722192e4436f617.html 文章中的第11页开始有划分phy和控制器部分....
- [NOI2008]设计路线
题目 洛谷 BZOJ 做法 神仙题 显然这是棵树 个节点相东仅连接一个结点 不同于剖分,还能存在\("V"\)字型,一个节点最多与另外节点连两条边 \(dp[i][j][k]\)表 ...
- 在vim下按ctrl+s后界面卡住
用惯了window编辑器的我们,在使用linux vim编辑器时会不会遇到这个问题:在编辑时总是会不小心按下Ctrl+S,然后整个终端都没有反应了?其实在Linux下 Ctrl+S是有特殊的用途的,不 ...