-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializeBT.cpp
74 lines (63 loc) · 1.61 KB
/
serializeBT.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
//
// serializeBT.cpp
// xcode
//
// Created by Gokul Nadathur on 8/30/16.
// Copyright © 2016 Gokul Nadathur. All rights reserved.
//
#include <string>
using namespace std;
/**
* 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:
int getVal(string& s, int& index) {
auto res = 1;
if (s[index] == '-') {
index ++;
res = -1;
}
res *= (s[index] - '0');
index++;
return res;
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if (root == NULL) {
return string(1, '0');
}
auto s = serialize(root->left);
s += serialize(root->right);
s = to_string(root->val) + s;
return s;
}
TreeNode* deserInternal(string& data, int& index) {
if (data.length() == 0 || index >= data.length()) {
return NULL;
}
if (data[index] == '0') {
index ++;
return NULL;
}
auto val = getVal(data, index);
auto n = new TreeNode(val);
n->left = deserInternal(data, index);
n->right = deserInternal(data, index);
return n;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int index = 0;
return deserInternal(data, index);
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));