-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializeandDeserializeBST.cpp
More file actions
46 lines (41 loc) · 1.17 KB
/
serializeandDeserializeBST.cpp
File metadata and controls
46 lines (41 loc) · 1.17 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
// Source: https://leetcode.com/problems/serialize-and-deserialize-bst/
// Author: Miao Zhang
// Date: 2021-02-12
/**
* 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:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if (!root) return "#";
return to_string(root->val) + ' ' + serialize(root->left) + ' ' + serialize(root->right);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
stringstream ss(data);
return build(ss);
}
private:
TreeNode* build(stringstream& ss) {
string elem;
ss >> elem;
if (elem == "#") return nullptr;
TreeNode* root = new TreeNode(stoi(elem));
root->left = build(ss);
root->right = build(ss);
return root;
}
};
// Your Codec object will be instantiated and called as such:
// Codec* ser = new Codec();
// Codec* deser = new Codec();
// string tree = ser->serialize(root);
// TreeNode* ans = deser->deserialize(tree);
// return ans;