-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay-003.cpp
65 lines (62 loc) · 1.54 KB
/
Day-003.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
queue<string>q;
TreeNode* desr(void) {
if (q.empty()) {
return nullptr;
}
string temp = q.front();
q.pop();
if (temp =="&") {
return nullptr;
}
TreeNode* root = new TreeNode(atoi(temp.c_str()));
root -> left = desr();
root -> right = desr();
return root;
}
string s;
void h_ser(TreeNode* root) {
if (root == nullptr) {
s += "&/";
return ;
}
s += to_string(root->val) + "/";
h_ser(root->left);
h_ser(root->right);
}
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
s = "";
h_ser(root);
return s;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data, int index = 0) {
string temp{};
for (int i=0; i<data.size(); ++i) {
if (data[i] == '/') {
q.push(temp);
temp ="";
continue;
}
temp += data[i];
}
return desr();
}
};
// Your Codec object will be instantiated and called as such:
// Codec* ser = new Codec();
// Codec* deser = new Codec();
// string tree = ser->serialize(root);
// TreeNode* ans = deser->deserialize(tree);
// return ans;