-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-SerializeAndDeserializeBinaryTree.cpp
More file actions
84 lines (72 loc) · 2.14 KB
/
Copy pathLeetCode-SerializeAndDeserializeBinaryTree.cpp
File metadata and controls
84 lines (72 loc) · 2.14 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
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if (root == NULL) return "";
string bits = "";
queue<TreeNode*> q;
q.push(root);
bits += to_string(root->val);
bits += " ";
while (!q.empty()) {
TreeNode * u = q.front();
q.pop();
if (u->left != NULL) {
q.push(u->left);
bits += to_string(u->left->val);
bits += " ";
} else {
bits += "#";
bits += " ";
}
if (u->right != NULL) {
q.push(u->right);
bits += to_string(u->right->val);
bits += " ";
} else {
bits += "#";
bits += " ";
}
}
int k = -1;
for (int i = bits.size() - 1; i >= 0; --i) {
if (bits[i] != '#' && bits[i] != ' ') {
k = i + 1;
break;
}
}
bits.erase(k, bits.size());
return bits;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(const string& data) {
if (data.empty()) return NULL;
stringstream ss(data);
istream_iterator<string> begin(ss);
istream_iterator<string> end;
vector<string> D(begin, end);
queue<TreeNode*> q;
TreeNode * root = new TreeNode(stoi(D[0]));
q.push(root);
int i = 1;
while (!q.empty()) {
TreeNode * u = q.front();
q.pop();
if (i >= D.size()) break;
if (D[i] != "#") {
TreeNode * v = new TreeNode(stoi(D[i]));
u->left = v;
q.push(v);
}
if (i + 1 < D.size()) {
if (D[i+1] != "#") {
TreeNode * v = new TreeNode(stoi(D[i+1]));
u->right = v;
q.push(v);
}
}
i+=2;
}
return root;
}
};