代码改变世界

[LeetCode 297] Serialize and deserialize binary tree

2017-11-18 13:19  naturesound  阅读(174)  评论(0编辑  收藏  举报

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.

--------------------------------------------------------------------

此题有两种解法:一种是常规的用队列实现的解法; 一种是用C++的ostringstream函数的递归方法。后一种方法比较简练,值得学习:

第一种:

队列
 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 public:
12 
13     // Encodes a tree to a single string.
14     string serialize(TreeNode* root) {
15         string res;
16         if(!root) return res;
17         queue<TreeNode*> q;
18         q.push(root);
19         while(!q.empty()){
20             TreeNode *cur = q.front();
21             q.pop();
22             if(!cur) res += "#,";
23             else{
24                 res += to_string(cur->val) + ",";
25                 q.push(cur->left);
26                 q.push(cur->right);
27             }
28         }
29         cout << res;
30         return res;
31     }
32 
33     // Decodes your encoded data to tree.
34     TreeNode* deserialize(string data) {
35         TreeNode *root = nullptr;
36         if(data.empty()) return root;
37         queue<TreeNode*> q;
38         for(int i=0; i<data.size(); i++){
39             int pos = data.find_first_of(',',i);
40             string cur = data.substr(i, pos-i);
41             if(q.empty()){
42                 root = new TreeNode(stoi(cur));
43                 q.push(root);
44                 i = pos;
45             }else{
46                 TreeNode *node = q.front();
47                 q.pop();
48                 if(cur != "#"){
49                     TreeNode *newnode = new TreeNode(stoi(cur));
50                     node->left = newnode;
51                     q.push(newnode);
52                 }
53                 int pos2 = data.find_first_of(',',pos+1);
54                 cur = data.substr(pos+1, pos2-pos-1);
55                 if(cur != "#"){
56                     TreeNode *newnode = new TreeNode(stoi(cur));
57                     node->right = newnode;
58                     q.push(newnode);
59                 }
60                 i = pos2;
61             }
62         }
63         return root;
64     }
65 };
66 
67 // Your Codec object will be instantiated a
解法

第二种:

 1 class Codec {
 2 public:
 3 
 4     string serialize(TreeNode* root) {
 5         ostringstream out;
 6         serialize(root, out);
 7         return out.str();
 8     }
 9 
10     TreeNode* deserialize(string data) {
11         istringstream in(data);
12         return deserialize(in);
13     }
14 
15 private:
16 
17     void serialize(TreeNode* root, ostringstream& out) {
18         if (root) {
19             out << root->val << ' ';
20             serialize(root->left, out);
21             serialize(root->right, out);
22         } else {
23             out << "# ";
24         }
25     }
26 
27     TreeNode* deserialize(istringstream& in) {
28         string val;
29         in >> val;
30         if (val == "#")
31             return nullptr;
32         TreeNode* root = new TreeNode(stoi(val));
33         root->left = deserialize(in);
34         root->right = deserialize(in);
35         return root;
36     }
37 };