-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0449_Codec.cc
110 lines (102 loc) · 2.66 KB
/
Problem_0449_Codec.cc
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
#include <iostream>
#include <string>
#include <vector>
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)
{
string ans;
vector<int> arr;
postOrder(root, arr);
if (arr.size() == 0)
{
return ans;
}
for (int i = 0; i < arr.size(); i++)
{
ans.append(std::to_string(arr[i]) + ",");
}
ans.pop_back(); // cut ',' at back
return ans;
}
void postOrder(TreeNode *cur, vector<int> &arr)
{
if (cur == nullptr)
{
return;
}
postOrder(cur->left, arr);
postOrder(cur->right, arr);
arr.push_back(cur->val);
}
// Decodes your encoded data to tree.
TreeNode *deserialize(string data)
{
if (data.size() == 0)
{
return nullptr;
}
vector<int> arr = split(data, ',');
return construct(INT32_MIN, INT32_MAX, arr);
}
vector<int> split(const string &str, char dec)
{
vector<int> ans;
int i = 0;
int start = 0;
while (i < str.size())
{
while (i < str.size() && str[i] == dec)
{
i++;
}
start = i;
while (i < str.size() && str[i] != dec)
{
i++;
}
if (start < str.size())
{
ans.emplace_back(stoi(str.substr(start, i - start)));
}
}
return ans;
}
// arr = [1, 3, 2]
// 当 value = 2 时,符合区间 (INT32_MIN, INT32_MAX),限定左边值区间(INT32_MIN, 2),限定右边值区间 (2, INT32_MAX)
// 当 value = 3 时,符合区间 (2, INT32_MAX)(即 2 的右子节点),限定左边值区间(2, 3),限定右边值区间 (3, INT32_MAX)
// 当 value = 1 时,不符合区间(2, 3),也不符合区间(3, INT32_MAX),所以不是 value = 3 的子节点
// 再次尝试 value = 1 时,符合区间 (INT32_MIN, 2),说明是 2 的左子节点
TreeNode *construct(int lower, int upper, vector<int> &arr)
{
if (arr.size() == 0 || arr.back() < lower || arr.back() > upper)
{
// 注意,这里的 lower 和 upper 非常关键
return nullptr;
}
int value = arr.back();
arr.pop_back();
TreeNode *node = new TreeNode(value);
// 右子树所有的值必须 > value
node->right = construct(value, upper, arr);
// 左子树所有的值必须 < value
node->left = construct(lower, value, arr);
return node;
}
};
// 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;