-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree_serialization.cpp
158 lines (141 loc) · 2.93 KB
/
binary_tree_serialization.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
#define PATH false
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {}
};
void Print(TreeNode* root)
{
if (root)
{
cout << root->val << " ";
Print(root->left);
Print(root->right);
}
}
class Codec
{
#if PATH
public:
vector<string> Serialize(TreeNode* root)
{
vector<string> ret;
if (root == nullptr) return ret;
std::queue<TreeNode*> q;
q.push(root);
while (!q.empty())
{
TreeNode* cur = q.front();
q.pop();
if (cur == nullptr)
{
ret.emplace_back("null");
}
else
{
ret.emplace_back(std::to_string(cur->val));
q.push(cur->left);
q.push(cur->right);
}
}
while (!ret.empty() && ret.back() == "null") ret.pop_back();
return ret;
}
TreeNode* Deserialize(const vector<string>& data)
{
if (data.empty() || data.front() == "null")
return nullptr;
TreeNode* root = new TreeNode(std::stoi(data.front()));
std::queue<TreeNode*> q;
q.push(root);
for (size_t i = 1; i < data.size(); ++i)
{
if (data[i] != "null")
{
}
}
}
private:
void do_serialize(TreeNode* root, vector<string>* o)
{
if (root)
{
o->emplace_back(std::to_string(root->val));
do_serialize(root->left, o);
do_serialize(root->right, o);
}
else
{
o->emplace_back("null");
}
}
TreeNode* do_deserialize(const vector<string>& data, int idx)
{
if (idx >= (int)data.size() || data[idx] == "null")
return nullptr;
TreeNode* root = new TreeNode(std::stoi(data[idx]));
root->left = do_deserialize(data, idx + 1);
root->right = do_deserialize(data, idx + 2);
return root;
}
#else
public:
string Serialize(TreeNode* root)
{
ostringstream os;
do_serialize(root, os);
return os.str();
}
TreeNode* Deserialize(const string& data)
{
istringstream is(data);
return do_deserialize(is);
}
private:
void do_serialize(TreeNode* root, ostream& os)
{
if (root)
{
os << root->val << ' ';
do_serialize(root->left, os);
do_serialize(root->right, os);
}
else
{
os << '#' << ' ';
}
}
TreeNode* do_deserialize(istream& is)
{
string val;
is >> val;
if (val == "#") return nullptr;
TreeNode* root = new TreeNode(std::stoi(val));
root->left = do_deserialize(is);
root->right = do_deserialize(is);
return root;
}
#endif
};
// test
int main()
{
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->right->left = new TreeNode(4);
root->right->right = new TreeNode(5);
cout << "The preorder tranverse of the tree is:\n";
Print(root);
Codec c;
cout << "\nNow the tree is:\n";
Print(c.Deserialize(c.Serialize(root)));
return 0;
}