-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Tree Serialization.cpp
114 lines (102 loc) · 2.75 KB
/
Binary Tree Serialization.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
#include<iostream>
#include<unordered_map>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<set>
#include<stack>
#include<sstream>
using namespace std;
class TreeNode {
public:
int val;
TreeNode *left, *right;
TreeNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
};
class Solution {
public:
/**
* This method will be invoked first, you should design your own algorithm
* to serialize a binary tree which denote by a root node to a string which
* can be easily deserialized by your own "deserialize" method later.
*/
void help_serialize(TreeNode *p, string& result)
{
if (!p) {
result +="# ";
} else {
result = result + to_string(p->val) + " ";
help_serialize(p->left, result);
help_serialize(p->right, result);
}
}
string serialize(TreeNode *root) {
// write your code here
string result("");
help_serialize(root, result);
return result;
}
bool readNextToken(int& token, stringstream &ss, bool &isNumber)
{
string s;
ss >> s;
if(s.length() == 0) return false;
if(s == "#")
{
isNumber = false;
return true;
}
token = stoi(s, nullptr);
isNumber = true;
return true;
}
TreeNode* readBSTHelper(stringstream &ss) {
int token;
bool isNumber;
TreeNode *p(nullptr);
if (!readNextToken(token, ss, isNumber))
return nullptr;
if (isNumber) {
p = new TreeNode(token);
p->left = readBSTHelper(ss);
p->right = readBSTHelper(ss);
}
return p;
}
/**
* This method will be invoked second, the argument data is what exactly
* you serialized at method "serialize", that means the data is not given by
* system, it's given by your own serialize method. So the format of data is
* designed by yourself, and deserialize it here as you serialize it in
* "serialize" method.
*/
TreeNode *deserialize(string data) {
// write your code here
stringstream ss(data);
TreeNode* root = readBSTHelper(ss);
return root;
}
};
int main()
{
TreeNode *n1 = new TreeNode(1);
TreeNode *n2 = new TreeNode(2);
TreeNode *n3 = new TreeNode(20);
TreeNode *n4 = new TreeNode(15);
TreeNode *n5 = new TreeNode(7);
//n1 -> left = n2;
n1 ->right = n2;
//n3 ->left = n4;
//n3 ->right = n5;
Solution s;
string res = s.serialize(n1);
cout << res << endl;
TreeNode* root = s.deserialize(res);
cout << s.serialize(root);
return 0;
}