[LeetCode] Serialize and Deserialize Binary Tree
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.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
Credits:
Special thanks to @Louis1992 for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
/**
* 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 {
private:
void _serialize(TreeNode *root, ostringstream &sout) {
if (root == NULL) {
sout << "# ";
return;
}
sout << root->val << " ";
_serialize(root->left, sout);
_serialize(root->right, sout);
}
void _deserialize(TreeNode *&root, istringstream &sin) {
int val;
string tmp;
if (sin >> tmp && tmp != "#") {
val = stoi(tmp);
} else {
return;
}
root = new TreeNode(val);
_deserialize(root->left, sin);
_deserialize(root->right, sin);
}
public: // Encodes a tree to a single string.
string serialize(TreeNode* root) {
ostringstream sout;
_serialize(root, sout);
return sout.str();
} // Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
istringstream sin(data);
TreeNode *root = NULL;
_deserialize(root, sin);
return root;
}
}; // Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
[LeetCode] Serialize and Deserialize Binary Tree的更多相关文章
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- [LeetCode] Serialize and Deserialize N-ary Tree N叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 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 ...
- LeetCode OJ: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
题目: Serialization is the process of converting a data structure or object into a sequence of bits so ...
随机推荐
- (转)EntityFrameword “Reverse Engineer Code First” 连接 MySql
转自:http://stackoverflow.com/questions/19676624/error-trying-to-reverse-engineer-code-first-mysql-dat ...
- 关闭 selinux 和防火墙
1.关闭 selinux 修改 它的配置文档 /etc/selinux/conf 修改 SELINUX=disabled 或者permissive 2. 关闭 防火墙 输入命令 systemctl d ...
- pro生成sln
跳转到对应的工程目录,通过执行如下的命令:qmake -tp vc 命令实现
- CLR内存管理
CLR管理内存的区域,主要有三块,分别为: 1.线程的堆栈:(在程序应该编译过程为值类型实例分配好内存) 用于分配值类型实例.堆栈主要由操作系统管理,而不受垃圾收集器的控制,当值类型实例所在方法结束时 ...
- codeforces 425E
题意:对于[l1, r1], [l2, r2]...[lm, rm]线段组成的一个集合S,我们定义f(S)为最大的不相交(没有任何公共点)线段数,现在给定n及k,n表示线段范围,即任何[li, ri] ...
- Webpack使用教程三(webpack-dev-server)
Webpack给本地开发提供了一个可选的服务器webpack-dev-server.webpack-dev-server是一个很小的express应用,使用前需要用npm安装,它根据webpack.c ...
- Zabbix3.0 自动电话报障
第一种:Pagerduty 网站:www.pagerduty.com 优点:老牌服务商,稳定 缺点:贵,英文,网站要FQ 价格参考(34美元每月才25个电话,*29每月是包年才有的价格) 安装方式: ...
- TypeScript 0.9.1 发布,新增 typeof 关键字
TypeScript 0.9.1 发布了,该版本提升了编译器和语言的性能,增加新的语言特性 typeof ,更好的 this 处理等.详细介绍请看发行说明. TypeScript 是微软新推出的一种语 ...
- 跟我一起学WCF(3)——利用Web Services开发分布式应用
一.引言 在前面文章中分别介绍了MSMQ和.NET Remoting技术,今天继续分享.NET 平台下另一种分布式技术——Web Services 二.Web Services 详细介绍 2.1 We ...
- 作业七:团队项目——Alpha版本冲刺阶段-06
昨天进展:代码编写. 今天安排:代码编写.