博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Serialize and Deserialize Binary Tree
阅读量:6692 次
发布时间:2019-06-25

本文共 2502 字,大约阅读时间需要 8 分钟。

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 . 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  for adding this problem and creating all test cases.

 

 to see which companies asked this question

 

1 /** 2  * Definition for a binary tree node. 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Codec {11 private:12     void _serialize(TreeNode *root, ostringstream &sout) {13         if (root == NULL) {14             sout << "# ";15             return;16         }17         sout << root->val << " ";18         _serialize(root->left, sout);19         _serialize(root->right, sout);20     }21     void _deserialize(TreeNode *&root, istringstream &sin) {22         int val;23         string tmp;24         if (sin >> tmp && tmp != "#") {25             val = stoi(tmp);26         } else {27             return;28         }29         root = new TreeNode(val);30         _deserialize(root->left, sin);31         _deserialize(root->right, sin);32     }33 public:34 35     // Encodes a tree to a single string.36     string serialize(TreeNode* root) {37         ostringstream sout;38         _serialize(root, sout);39         return sout.str();40     }41 42     // Decodes your encoded data to tree.43     TreeNode* deserialize(string data) {44         istringstream sin(data);45         TreeNode *root = NULL;46         _deserialize(root, sin);47         return root;48     }49 };50 51 // Your Codec object will be instantiated and called as such:52 // Codec codec;53 // codec.deserialize(codec.serialize(root));

 

转载地址:http://iicoo.baihongyu.com/

你可能感兴趣的文章
让元素居中
查看>>
php memcache保存session的一个设置误区
查看>>
鱼眼镜头
查看>>
Scalatra
查看>>
CentOS 7 三者分离编译安装LAMP
查看>>
Linux内核调整,支持4000-8000并发
查看>>
jquery mobile 设置设备适配
查看>>
redis使用总结-redis命令总结
查看>>
创业浪潮:春天蓬勃而来
查看>>
阿里云Linux安装软件镜像源
查看>>
阿里云对象存储OSS支持版本管理特性
查看>>
用python 访问redis的几种常用方式
查看>>
我的友情链接
查看>>
Linux Shell 基本概念及编程(5)
查看>>
RDBMS DBMS MS DB
查看>>
我的友情链接
查看>>
svn 实践
查看>>
在 PowerShell 中使用 SQL Server (3)
查看>>
我的友情链接
查看>>
CSS元素定位
查看>>