-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path62_SerializeBinaryTrees.cpp
More file actions
149 lines (116 loc) · 2.62 KB
/
Copy path62_SerializeBinaryTrees.cpp
File metadata and controls
149 lines (116 loc) · 2.62 KB
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
# include <iostream>
# include <vector>
# include <cstdlib>
# include <string>
# include <cstring>
using namespace std;
// definition of tree node
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void dispaly_vec(vector<int> &vec)
{
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << ' ';
cout << endl;
}
void dispaly_tree(TreeNode* root)
{
if (root == NULL)
return;
cout << root->val << ' ';
dispaly_tree(root->left);
dispaly_tree(root->right);
}
void Serialize(TreeNode *root, string &res)
{
if (root == NULL)
{
res += "$ ";
return;
}
res += (to_string(root->val) + " ");
Serialize(root->left, res);
Serialize(root->right, res);
}
char* Serialize(TreeNode *root)
{
string res_str;
Serialize(root, res_str);
res_str.pop_back();
// convert to char
char *res = new char[res_str.size() + 1];
strcpy(res, res_str.c_str());
return res;
}
TreeNode* Deserialize(vector<TreeNode *> &series, int &pos)
{
if (series.size() <= 0 || pos < 0 || pos >= series.size())
return NULL;
if (series[pos] == NULL)
{
pos += 1;
return NULL;
}
TreeNode *root = series[pos]; pos += 1;
root->left = Deserialize(series, pos);
root->right = Deserialize(series, pos);
return root;
}
TreeNode* Deserialize(char *str)
{
if (str == NULL || *str == '\0')
return NULL;
// split string to node
vector<TreeNode *> series;
TreeNode *node;
char *p = str, *q = str;
int val;
while (*p != '\0')
{
// find end
for (q = p; *q != '\0' && *q != ' '; q++) ;
// string to node
string t(p, q - p);
if (t[0] != '$')
{
val = atoi(t.c_str());
node = new TreeNode(val);
}
else
node = NULL;
series.push_back(node);
// find start
for (p = q; *p != '\0' && *p == ' '; p++) ;
}
// de-serialize
int pos = 0;
return Deserialize(series, pos);
}
int main()
{
TreeNode *root = NULL, *p, *root_de;
char *res;
// construct tree
root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
p = root->left;
p->left = new TreeNode(4);
p = root->right;
p->left = new TreeNode(5);
p->right = new TreeNode(6);
dispaly_tree(root);
cout << endl << endl;
// result
res = Serialize(root);
cout << res << endl;
root_de = Deserialize(res);
dispaly_tree(root_de);
cout << endl;
return 0;
}