-
Notifications
You must be signed in to change notification settings - Fork 1
/
449. Serialize and Deserialize BST.cpp
176 lines (140 loc) · 4.72 KB
/
449. Serialize and Deserialize BST.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* 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 {
public:
string s = "";
void serDfs(TreeNode* root) {
if (root == nullptr) return;
s += to_string(root->val) + ",";
if (root->left != nullptr) serDfs(root->left);
if (root->right != nullptr) serDfs(root->right);
return;
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
serDfs(root);
cout << s << endl;
return s;
}
void stringSplit(string str, char split, vector<int>& nums)
{
istringstream iss(str); // 输入流
string token; // 接收缓冲区
while (getline(iss, token, split)) // 以split为分隔符
{
nums.push_back(atoi(token.c_str()));
cout << token << endl; // 输出
}
}
TreeNode* buildTree(const vector<int> &pre, int preS, int preE,
const vector<int> &in, int inS, int inE) {
if (preS > preE || inS > inE) return nullptr;
TreeNode* root = new TreeNode(pre[preS]);
int midIdx = -1;
for (int i = inS; i <= inE; i++) {
if (in[i] == pre[preS]) {
midIdx = i;
break;
}
}
root->left = buildTree(pre, preS + 1, preS + midIdx - inS,
in, inS, midIdx - 1);
root->right = buildTree(pre, preS + midIdx - inS + 1, preE,
in, midIdx + 1, inE);
return root;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if (data == "") return nullptr;
vector<int> preNums;
stringSplit(data, ',', preNums);
vector<int> inNums = preNums;
sort(inNums.begin(), inNums.end());
return buildTree(preNums, 0, preNums.size() - 1,
inNums, 0, inNums.size() - 1);
}
};
// 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;
/**
* 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 {
public:
void stringSplit(string str, char split, vector<int>& nums)
{
istringstream iss(str); // 输入流
string token; // 接收缓冲区
while (getline(iss, token, split)) // 以split为分隔符
{
nums.push_back(atoi(token.c_str()));
//cout << token << endl; // 输出
}
}
void dfs(TreeNode* root, string &s) {
if (root == nullptr) return;
s += to_string(root->val) + "+";
dfs(root->left, s);
dfs(root->right, s);
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string s = "";
dfs(root, s);
//cout << s << endl;
return s;
}
TreeNode* buildTree(vector<int> &pre, int preS, int preE,
vector<int> &in, int inS, int inE)
{
if (preS > preE || inS > inE) return nullptr;
TreeNode* root = new TreeNode(pre[preS]);
int midIdx = -1;
for (int i = inS; i <= inE; i++) {
if (in[i] == pre[preS]) {
midIdx = i;
//cout << midIdx << endl;
break;
}
}
//cout << preS + 1 << " " << preS + midIdx - inS << endl;
root->left = buildTree(pre, preS + 1, preS + midIdx - inS,
in, inS, midIdx - 1);
root->right = buildTree(pre, preS + midIdx - inS + 1, preE,
in, midIdx + 1, inE);
return root;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
vector<int> pre;
stringSplit(data, '+', pre);
vector<int> in = pre;
sort(in.begin(), in.end());
TreeNode* root = buildTree(pre, 0, pre.size() - 1,
in, 0, in.size() - 1);
return root;
}
};
// 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;