序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。
请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列/反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且这个字符串可以被反序列化得到一个原始的树结构。
例如,你可以序列化下面的二叉树:
    1
   / \
  2   3
     / \
    4   5

得到 "[1,2,3,null,null,4,5]",这与LeetCode目前使用的方式一致 how LeetCode OJ serializes a binary tree。你并非必须采取这种方式,你也可以创造性的用其他的方式解决这个问题。
小贴士: 不要使用类的成员/全局/静态变量来存储状态机,你的序列化和反序列化算法应该是无状态机的。

详见:https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/

/**
* 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) {
ostringstream out;
serialize(root,out);
return out.str();
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
istringstream in(data);
return deserialize(in);
}
private:
void serialize(TreeNode* root,ostringstream &out)
{
if(root)
{
out<<root->val<<' ';
serialize(root->left,out);
serialize(root->right,out);
}
else
{
out<<"# ";
}
}
TreeNode* deserialize(istringstream &in)
{
string val;
in>>val;
if(val=="#")
{
return nullptr;
}
TreeNode *root=new TreeNode(stoi(val));
root->left=deserialize(in);
root->right=deserialize(in);
return root;
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

参考:https://www.cnblogs.com/grandyang/p/4913869.html

297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化的更多相关文章

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

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

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

  3. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

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

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

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

  5. LC 297 Serialize and Deserialize Binary Tree

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

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

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

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

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

  9. Leetcode 297. Serialize and Deserialize Binary Tree

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

随机推荐

  1. 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle

    http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007&noticeId=15020 求出n个数的和sum,然后用s ...

  2. JavaScript高级篇之Function对象

    JavaScript高级篇之Function对象 一: Function对象引入: Function对象是js的方法对象,可以用Function实例化出任何js方法对象. 例如: <%@ pag ...

  3. Bootstrap官网文档查询

    Ctrl+F 在出现的小搜索框里面输入要查找的东西.回车即可!

  4. 深入理解hadoop(二)

    hadoop RPC 网络通信是hadoop的核心模块之一,他支撑了整个Hadoop的上层分布式应用(HBASE.HDFS.MapReduce), Hadoop RPC具有以下几个特性,透明性(用户本 ...

  5. 洛谷 P1023 税收与补贴问题

    P1023 税收与补贴问题 题目背景 每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低于成本销售),并假设相邻价位间销量的变化是线性的且在价格高于给定的最 ...

  6. Why It is so hard to explain or show some thing

    Why it is hard to explain something or learn something? For example, when I first know the hadoop, I ...

  7. json序列化后的是字符串,不是二进制。是字符串!!!确定不是二进制!!!

    1.现有一个自定义对象需要储存到sql数据库中去.这个对象里面属性很多,甚至包含一些元素量打到几十万的List集合属性.本人试着使用JSON序列化这个对象,储存到数据库,报maxjsonlength超 ...

  8. dataTables 添加行内操作按钮

    在上一篇博客中我们提到了用dataTables 做一个分页表格.今天进一步进阶,做一个行内带操作按钮的表格.效果如图. 记得最基础的实现方式是,我们要在js 中拼接字符串,嵌入一个带按钮的语句.但是现 ...

  9. Outlook2010 没有Exchange Server,怎么自动回复邮件?

    步骤 1:创建邮件模板 单击“开始”>“新建邮件”. 在邮件正文中,键入要作为自动答复发送的邮件. 在邮件窗口中,单击“文件”>“另存为”. 在“另存为”对话框中的“保存类型”列表中,单击 ...

  10. SDUTOJ 2476Period

    #include<iostream> #include<string.h> #include<stdio.h> #define N 1000010 using na ...