[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 ...
随机推荐
- 中国排名前100的IT公司 (转)
排序 单位名称 软件收入 1 华为技术有限公司 622360 2 中兴通讯股份有限公司 601331 3 海信集团有限公司 448641 4 UT斯达康通讯有限公司 386763 5 海尔集团 ...
- 【排列组合】bzoj3505 [Cqoi2014]数三角形
http://blog.csdn.net/zhb1997/article/details/38474795 #include<cstdio> #include<algorithm&g ...
- OpenLayers中的图层
OpenLayers有多个不同的图层类,每一个都可以连接到不同的地图服务器.例如通过Layer.WMS类可以连接到WMS地图服务器,通过Layer.Google类可以连接到谷歌地图服务器.OpenLa ...
- Eclipse格式化代码快捷键失效问题
一般情况下,Eclipse快捷键失效是因为与其它软件快捷键冲突,Eclipse格式化代码快捷键正好与搜狗输入法的“简繁切换”的快捷键冲突,将搜狗输入法的快捷键修改一下就行了.
- opengl的mipmap
压缩纹理是不能调用glGenerateMipmap生成mipmap的. DDS和PVR都不行. 强行调用会产生GL_INVALID_OPERATION的错误. PNG格式试验了glGenerateMi ...
- 理解 Soap
http://www.cnblogs.com/yhuang/archive/2012/04/04/share_storm.html 自己也写了下: using System; using System ...
- PayPal贝宝集成
今天在集成PayPal贝宝在线支付功能时,遇到了一些小挫折,费了不少功夫才最终解决(贝宝的技术支持确实让我很想吐槽).现在记录下来,供后来者参考.根据集成说明文档,我们写的测试demo如下: < ...
- SQL Server 性能优化之——T-SQL NOT IN 和 NOT Exists
这次介绍一下T-SQL中“Not IN” 和“Not Exists”的优化. Not IN 和 Not Exists 命令 : 有些情况下,需要select/update/delete 操作孤立数据. ...
- 专题:点滴Javascript
JS#38: Javascript中递归造成的堆栈溢出及解决方案 JS#37: 使用console.time测试Javascript性能 JS#36: Javascript中判断两个日期相等 JS#3 ...
- 《区块链:从入门到放弃》之obc安装步骤
obc安装步骤 朋友们可能会好奇,厨师不研究菜谱怎么改研究兵法了,哈哈,我原本是app出身,最近被安排去预研区块链和比特币技术,2个月下来,颇有斩获.期间得到IBM的CC同学指导我一步一步安装obc的 ...