forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialize and Deserialize BST.cpp
83 lines (70 loc) · 1.97 KB
/
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
class Codec
{
public:
string pre(TreeNode * root)
{
if(root==NULL)
return "";
int temp = root->val;
string tempz = to_string(temp);
string l = pre(root->left);
string r = pre(root->right);
return (tempz + "," + l + "," + r);
}
vector<int> parse_string(string data)
{
vector<int> ans;
string temp = "";
int n=data.size();
for(int i=0;i<n; ++i)
{
if(data[i]==',')
{
if(temp.size()==0)
continue;
else
{
int val = stoi(temp);
ans.push_back(val);
temp="";
}
}
else
temp += data[i];
}
return ans;
}
TreeNode * make_tree(vector<int>& preorder, int min_val, int max_val, int& preorder_idx, int n)
{
if(preorder_idx == n)
return NULL;
int val = preorder[preorder_idx];
if(min_val <= val and val <= max_val)
{
TreeNode * curr = new TreeNode(val);
preorder_idx++;
curr->left = make_tree(preorder, min_val, val-1, preorder_idx, n);
curr->right = make_tree(preorder, val+1, max_val, preorder_idx, n);
return curr;
}
else
return NULL;
}
string serialize(TreeNode* root)
{
string traversal = pre(root);
//cout<<traversal<<endl;
return traversal;
}
TreeNode* deserialize(string data)
{
if(data.size()==0)
return NULL;
vector<int> traversal = parse_string(data);
// for(auto x:traversal)
// cout<<x<<" ";
int preorder_idx = 0;
TreeNode * root = make_tree(traversal, INT_MIN, INT_MAX, preorder_idx, traversal.size());
return root;
}
};