-
Notifications
You must be signed in to change notification settings - Fork 118
/
297.cpp
132 lines (117 loc) · 3.65 KB
/
297.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
#include <iostream>
#include <queue>
#include <string>
#include <cassert>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if (root == NULL) return "";
queue<TreeNode *> q;
q.push(root);
TreeNode *p;
string result = to_string(root->val);
while (!q.empty()) {
p = q.front();
q.pop();
if (p->left && p->right) {
q.push(p->left);
q.push(p->right);
result += "," + to_string(p->left->val) + "," + to_string(p->right->val);
}
else if (p->left && !p->right) {
q.push(p->left);
result += ",L," + to_string(p->left->val);
}
else if (!p->left && p->right) {
q.push(p->right);
result += ",R," + to_string(p->right->val);
}
else {
result += ",N";
}
}
return result;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if (data.length() == 0) return NULL;
vector<string> nodes = split(data, ',');
auto it = nodes.begin();
TreeNode *root = new TreeNode(stoi(*(it++)));
queue<TreeNode *> q;
q.push(root);
while (!q.empty() && it != nodes.end()) {
TreeNode *p = q.front();
if (*it == "N") {
it++;
}
else if (*it == "L") {
p->left = new TreeNode(stoi(*(++it)));
it++;
}
else if (*it == "R") {
p->right = new TreeNode(stoi(*(++it)));
it++;
}
else {
p->left = new TreeNode(stoi(*(it++)));
p->right = new TreeNode(stoi(*(it++)));
}
q.pop();
if (p->left) q.push(p->left);
if (p->right) q.push(p->right);
}
return root;
}
vector<string> split(string str, char delimiter) {
vector<string> result;
int last = 0;
for (int i = 0; i <= str.length(); i++) {
if (str[i] == delimiter || str[i] == '\0') {
result.push_back(str.substr(last, i - last));
last = i + 1;
}
}
return result;
}
};
bool compareTree(TreeNode *a, TreeNode *b) {
if (a == NULL && b == NULL) return true;
if (a == NULL || b == NULL) return false;
return (a->val == b->val)
&& compareTree(a->left, b->left)
&& compareTree(a->right, b->right);
}
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
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);
root->right->left->left = new TreeNode(6);
root->right->right->right = new TreeNode(7);
// 1
// / \
// 2 3
// / \
// 4 5
// / \
// 6 7
// serialization: 1,2,N,3,4,5,L,6,R,7,N,N
Codec codec;
TreeNode *new_root = codec.deserialize(codec.serialize(root));
assert(compareTree(root, new_root) == true);
printf("all tests passed!\n");
return 0;
}