297. Serialize and Deserialize Binary Tree *HARD*
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.
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) {
string s = "";
queue<TreeNode*> q;
if(root)
q.push(root);
while(!q.empty())
{
TreeNode *p = q.front();
q.pop();
if(!p)
{
s += "null ";
}
else
{
s += to_string(p->val) + " ";
q.push(p->left);
q.push(p->right);
}
}
cout<<s<<endl;
return s;
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
cout<<data<<endl;
if(data == "")
return NULL;
int l = data.length(), idx1 = , idx2;
queue<TreeNode*> q;
TreeNode *root = NULL;
bool flag = ; //0表示该左子树,1表示该右子树
while(idx1 < l)
{
idx2 = data.find(" ", idx1+);
string s = data.substr(idx1, idx2-idx1);
if(s == "null")
{
if(flag)
{
q.front()->right = NULL;
q.pop();
}
else
q.front()->left = NULL;
flag = !flag;
}
else
{
int t = atoi(s.c_str());
TreeNode *p = new TreeNode(t);
if(!root)
root = p;
else
{
if(flag)
{
q.front()->right = p;
q.pop();
}
else
q.front()->left = p;
flag = !flag;
}
q.push(p);
}
idx1 = idx2+;
}
return root;
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
297. Serialize and Deserialize Binary Tree *HARD*的更多相关文章
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- 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二叉树的序列化和反序列化(就用Q)
[抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...
- 【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 ...
随机推荐
- mybatis-spring-boot-autoconfigure
mybatis-spring-boot-autoconfigure – MyBatis Sring-BootStarter | Reference Documentation http://www.m ...
- Mybatis的WHERE和IF动态
mapper.xml: <!--查询套餐产品 --> <select id="queryComboProducts" resultType="com.r ...
- mysql 删除表
删除表 DROP TABLE 表名;
- mysql 约束条件 auto_increment 自动增长起始值 布长 起始偏移量
我们指定一个字段为自动增长,他默认从1开始自动增长,默认值为1,每次增长为1,步长为1 模糊查询 like % 代表任意个数字符 任意字符长度 查看mysql正在使用变量 show variables ...
- 江苏新美星智能物流无人叉车AGV
新美星一家全球领先的液体包装解决方案供应商,高附加值的产品应用于食品饮料等行业,为液体食品和自动化系统提供完整解决方案.新美星,于CBST2017展会首次亮相了能够从仓库或工厂的某个地方把材料.托盘和 ...
- Randcher 2.0部署K8s集群(一)
环境准备 1.系统版本 CentOS7.5 + docker ee 2.配置阿里云yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirro ...
- soapUI-DataSource
1.1.1.1 概述 - 数据源 Option Description Properties DataSource属性表 Toolbar DataSource工具栏 Configura ...
- Websocket、长连接、循环连接
[转]转自知乎高票回答 https://www.zhihu.com/question/20215561 一.WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系 ...
- 如何使用OpenCart 2.x Event事件系统
如何使用OpenCart 2.x Event事件系统 OpenCart 2.x 包含很多新特性,其中之一就是专为开发者提供的事件系统,Event System.它允许你在不修改原有系统代码的基础上( ...
- PHP error_reporting() 错误控制函数功能详解
定义和用法:error_reporting() 设置 PHP 的报错级别并返回当前级别. 函数语法:error_reporting(report_level) 如果参数 level 未指定,当前报错级 ...