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

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  2. [转]Attribute在.net编程中的应用

    Attribute在.net编程中的应用(一) Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是 ...

  3. System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。

    错误背景: 操作系统:编程环境:VS2013.  语言:VB.net:  数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...

  4. JS 获取form表单的所有数据

    在HTML中用js获取通过GET.POST方法(就是在网址后加?a=b&c=d之类)传过来的表单值. 针对大家常用的获取表单方式,很多时候都是在重复的写一些代码,今天给大家贴出来的代码可以作为 ...

  5. Android studio 插件之 GsonFormat (自己主动生成javabean)

    概述 相信大家在做开发的过程中都写过非常多的javabean ,非常多情况下 都是一个列表数据就是一个单独的javabean.假设大家自己敲的话费时费力 还非常easy敲错. 今天给大家推荐一个插件 ...

  6. psping

    psping工具功能主要包括:ICMP Ping.TCP Ping.延迟测试.带宽测试,是微软出品. 下载地址:https://download.sysinternals.com/files/PSTo ...

  7. Write a program that gives count of common characters presented in an array of strings..(or array of

    转自出处 Write a program that gives count of common characters presented in an array of strings..(or arr ...

  8. HDU 5416

    CRB and Tree                                       Time Limit: 8000/4000 MS (Java/Others)    Memory ...

  9. Message: unknown error: Element is not clickable at point

    Message: unknown error: Element is not clickable at point google chrome - Debugging "Element is ...

  10. CANopen——总线基本知识

    1. 总线标准 2. 获取索引和子索引 2fh,2bh,23h,40h等,是不是对应cs的不同值: 主站1280h的对象字典?1280h-sub2,得到client的COB-ID值: 根据收到的m-& ...