-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (69 loc) · 1.48 KB
/
main.cpp
File metadata and controls
90 lines (69 loc) · 1.48 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
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <cstring>
#include <set>
#include <algorithm>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(nullptr), right(nullptr) {
}
};
char* Serialize(TreeNode *root) {
if (!root)
return "#";
std::string str = std::to_string(root->val);
str.push_back(',');
char *left = Serialize(root->left);
char *right = Serialize(root->right);
char *ret = new char[strlen(left) + strlen(right) + str.size()];
strcpy(ret, str.c_str());
strcat(ret, left);
strcat(ret, right);
return ret;
}
TreeNode *Deserialize(char **str)
{
if (**str == '#')
{
++(*str);
return nullptr;
}
int num = strtol(*str, str, 10);
TreeNode *node = new TreeNode(num);
if (**str == 0)
return node;
else
++(*str);
node->left = Deserialize(str);
node->right = Deserialize(str);
return node;
}
TreeNode *Deserialize(char *str)
{
if (!str)
return nullptr;
TreeNode *node = Deserialize(&str);
return node;
}
int main()
{
TreeNode *root = new TreeNode(8);
TreeNode *root1 = new TreeNode(6);
TreeNode *root2 = new TreeNode(10);
TreeNode *root3 = new TreeNode(5);
TreeNode *root4 = new TreeNode(7);
TreeNode *root5 = new TreeNode(9);
TreeNode *root6 = new TreeNode(11);
root->left = root1;
root->right = root2;
root1->left = root3;
root1->right = root4;
root2->left = root5;
root2->right = root6;
char * str = Serialize(root);
TreeNode *node = Deserialize(str);
return 0;
}