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 ...
- JS编写类似弹出窗口样式显示层
JSp中增加div <!-- 提交变更申请 --> <div id="changeWindow" class="easyui-window" ...
- Python 4 函数的参数,内置函数,装饰器,生成器,迭代器,
一.函数的参数: 1.位置参数:调用函数时根据函数定义的参数位置来传递参数. 2.关键字参数:用于函数调用,通过“键-值”形式加以指定.可以让函数更加清晰.容易使用,同时也清除了参数的顺序需求. 3. ...
- 字典,字符串,元组,字典,集合set,类的初步认识,深浅拷贝
Python之路[第二篇]:Python基础(一) 入门知识拾遗 一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'Jaso ...
- jQuery UI入门
jQuery UI是jQuery的一个插件集,为jQuery的核心库添加了新的功能. jQUery UI库可以从http://jquery.com下载. 下载一个ZIP文件jquery-ui-1.9. ...
- 对”唯一键可以包含NULL值,并且每个NULL值都是唯一的(即NULL!=NULL)“理解
因为最近在写一篇关于字符串模糊检索的论文,开始比较细致的研究数据库(MySQL)中的index问题,变到图书馆借了本<Effective MySQL之SQL语句最优化>(Ronald Br ...
- C语言math.h库函数中atan与atan2的区别
源: C语言math.h库函数中atan与atan2的区别 C语言中的atan和atan2
- JDK源码 - ArrayList (基于1.7)
前言 推荐一位大牛的博客: https://blog.csdn.net/eson_15/article/details/51121833 我基本都是看的他的源码分析,刚开始如果直接看jdk源码可能 ...
- 初识python---简介,简单的for,while&if
一编程语言:编程语言是程序员与计算机沟通的介质: 编程语言的分类: 1机器语言:是用二进制代码表示的计算机能直接识别和执行的一种机器指令的集合. 优点:灵活,直接执行和速度快 ...
- thinkphp 多表事务处理
try{ $this->user = D('User'); $this->user->startTrans(); //开始事务 $res = $this->user->S ...