-
Notifications
You must be signed in to change notification settings - Fork 1
/
297.二叉树的序列化与反序列化.cpp
86 lines (81 loc) · 1.96 KB
/
297.二叉树的序列化与反序列化.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
/*
* @lc app=leetcode.cn id=297 lang=cpp
*
* [297] 二叉树的序列化与反序列化
*/
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// @lc code=start
/**
* 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 str;
string nullToken = "null";
string delimiter = ",";
// Encodes a tree to a single string.
string serialize(TreeNode *root) {
preTravel(root);
return str;
}
void preTravel(TreeNode *root) {
if (root == nullptr) {
str += "null,";
return;
}
str += (root->val + ',');
preTravel(root->left);
preTravel(root->right);
}
// Decodes your encoded data to tree.
TreeNode *deserialize(string data) {
vector<string> list;
tokenize(data, ',', list);
// 前序遍历
return deserialize(list);
}
TreeNode *deserialize(vector<string> data) {
if (data.size() == 0)
return nullptr;
string str = data.at(0);
data.erase(data.begin());
if (str == "null") {
return nullptr;
}
TreeNode *root = new TreeNode(stoi(str));
root->left = deserialize(data);
root->right = deserialize(data);
return root;
}
// 实现split的功能
void tokenize(const string &s, const char delim, vector<string> &out) {
string::size_type beg = 0;
for (auto end = 0; (end = s.find(delim, end)) != string::npos; ++end) {
out.push_back(s.substr(beg, end - beg));
beg = end + 1;
}
out.push_back(s.substr(beg));
}
};
// Your Codec object will be instantiated and called as such:
// @lc code=end
int main() {
Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));
TreeNode *ans = deser.deserialize("1,2,null,null,3,4,5");
}