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*的更多相关文章

  1. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  2. LC 297 Serialize and Deserialize Binary Tree

    问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...

  3. 297. Serialize and Deserialize Binary Tree

    题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...

  4. 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 ...

  5. [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 ...

  6. 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)

    [抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...

  7. 【LeetCode】297. Serialize and Deserialize Binary Tree

    二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...

  8. 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化

    序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...

  9. Leetcode 297. Serialize and Deserialize Binary Tree

    https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of ...

随机推荐

  1. StartUML-时序图

  2. Spring 依赖注入 基于构造函数、设值函数、内部Beans、集合注入

    Spring 基于构造函数的依赖注入_w3cschool https://www.w3cschool.cn/wkspring/t7n41mm7.html Spring 基于构造函数的依赖注入 当容器调 ...

  3. 洛谷P1156 垃圾陷阱 dp

    正解:dp 解题报告: 这儿是传送门! 话说最近怎么神仙们都开始狂刷dp,,,感觉今天写了好多dp的题解的样子?(也就三四道其实× 然后这题,首先看到要么吃要么堆起来就会想到01背包趴?然后就考虑设方 ...

  4. yii2框架2 (二)项目结构

    原文 http://www.yiichina.com/doc/guide/2.0/structure-overview 应用结构 应用中最重要的目录和文件(假设应用根目录是 basic): basic ...

  5. 什么是虚拟DOM?

    (摘抄自一篇文章,觉得这里写得非常不错,所以单独放出来,希望能对大家有帮助.)React为啥这么大?因为它实现了一个虚拟DOM(Virtual DOM).虚拟DOM是干什么的?这就要从浏览器本身讲起 ...

  6. SQL Server分区键列必须是主键一部分

    SQL Server分区键列必须是主键一部分. 必须把分区列包含在主键/唯一约束/唯一索引的键列中. USE tempdb GO -- 测试表 CREATE TABLE dbo.tb( id int, ...

  7. 【Lua】面向对象编程(二)

      多重继承: module(...,package.seeall) local function search(k,plist) ,#plist do local v=plist[i][k] if ...

  8. make clean 与 make distclean 的区别

    make clean仅仅是清除之前编译的可执行文件及配置文件. 而make distclean要清除所有生成的文件. Makefile 在符合GNU Makefiel惯例的Makefile中,包含了一 ...

  9. 运行javac 报告javac不是内部或外部命令,但是运行java、java-version正常

    以前装jdk 从来没遇到过今天这种情况,各种解决办法试了一下午,终于出来了,说一下解决的办法: JAVA_HOME .classpath 都在系统变量中建立好: java_home 添加jdk的安装目 ...

  10. 【运维技术】CentOS7上从零开始安装LAMP安装织梦DedeCMS教程

    前期准备数据 centos7 系统 安装 appache httpd # 更新httpd yum update httpd # 安装httpd yum install -y httpd # 启动服务 ...