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 ...
随机推荐
- trait特性
1.trait特性可以和特化或者偏特化结合. 2.trait可以和类型转换结合.
- JS编写类似弹出窗口样式显示层
JSp中增加div <!-- 提交变更申请 --> <div id="changeWindow" class="easyui-window" ...
- NodeJS应用程序设置为window service-辅助工具(C#)
1.修改nssm,去对话框后 2.生成批处理文件,执行 3.将nssm.exe.node.exe放在资源文件里面 附代码 工具
- PAT 天梯赛 L1-037. A除以B 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-037 AC代码 #include <iostream> #include <cstdio&g ...
- Linux基本命令 vim命令(一)
vim的三种工作模式 命令模式.输入模式和编辑模式的相互转换,如图 命令模式:使用 Vim 编辑文件时,默认处于命令模式.在此模式下,可以使用上.下.左.右键或者 k.j.h.l 命令进行光标移动,还 ...
- mysql case的语法
测试表:team 第一种语法: CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_ ...
- Docker容器技术-Docker架构
一.Docker系统架构 1.Docker基础架构 1)Docker守护进程 负责容器的创建.运行和监控,以及镜像的构建和存储. docker daemon 2)Docker客户端 通过HTTP与Do ...
- weblogic启动错误 Unrecognized option: -jrockit
高版本jdk启动低版本weblogic有时会报Unrecognized option: -jrockit参数错误 这纯粹是版本问题,版本更新更换参数名称的缘故 解决方法: “%WL_HOME%\com ...
- java常用日期操作方法
package com.wujiangpo.test.util; import java.text.ParseException; import java.text.SimpleDateFormat; ...
- 乌云TOP 10 简单介绍
已知OWASP TOP10的WEB漏洞,乌云出了一个更加符合中国国情的 乌云:Top10 for 2014. A1-互联网泄密事件/撞库攻击 本质上来说是使用了不安全的口令,也许我可以将自己的密码设置 ...