-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode297.cpp
56 lines (49 loc) · 968 Bytes
/
leetcode297.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
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {}
};
class Codec
{
public:
// Encodes a tree to a single string.
string Serialize(TreeNode* root)
{
vector<string> ret;
do_serialize(root, &ret);
ostringstream os;
os << '[';
for (size_t i = 0; i < ret.size() - 1; ++i)
{
os << ret[i] << ',';
}
os << ret.back();
os << ']';
return os.str();
}
// Decodes a encoded data to tree.
TreeNode* Deserialize(const string& data);
public:
void do_serialize(TreeNode* root, vector<string>* o)
{
if (root)
{
o->emplace_back(std::to_string(root->val));
do_serialize(root->left, o);
do_serialize(root->right, o);
}
else
{
o->emplace_back("null");
}
}
void do_deserialize(const vector<string>& data)
{
}
};