-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize_deserialize.cpp
More file actions
66 lines (58 loc) · 1.64 KB
/
Copy pathserialize_deserialize.cpp
File metadata and controls
66 lines (58 loc) · 1.64 KB
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
#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <map>
#include <cmath>
/**
* Definition for a binary tree node.
* struct Node {
* int value;
* Node *left;
* Node *right;
* Node(int val, Node *left, Node *right) : value(val), left(left), right(right) {}
* };
*/
string serialize(Node* root)
{
string encoded_str = "";
if(root == NULL)
{
encoded_str = "* "; // differentiating end of Tree by *
}
else
{
encoded_str = encoded_str + root->value + " "; // Space separated values in pre-order traversal
encoded_str = encoded_str + serialize(root->left);
encoded_str = encoded_str + serialize(root->right);
}
return encoded_str;
}
string extractFirstNumberAndModifyString(string& input) {
// Find the position of the first space
int pos = input.find(" ");
// If there is no space, the whole string is a number
if (pos == string::npos) {
firstNumber = input;
input = "";
} else {
// Extract the first number
firstNumber = input.substr(0, pos);
// Modify the original string to exclude the first number and the space
input = input.substr(pos + 1);
}
return firstNumber;
}
Node* deserialize(string encoded_input)
{
string value = extractFirstNumberAndModifyString(encoded_input);
if(value == "*")
{
return NULL;
}
else
{
Node* root = new Node(stoi(value), deserialize(encoded_input), deserialize(encoded_input));
// extracting values in pre-order traversal
}
return root;
}