Skip to content

Commit 5b8b656

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <[email protected]>
1 parent e09b73c commit 5b8b656

File tree

2 files changed

+92
-34
lines changed

2 files changed

+92
-34
lines changed

0297_serialize_and_deserialize_binary_tree/tree_serdes.c

+19-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdbool.h>
1+
#include <limits.h>
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <string.h>
@@ -10,52 +10,37 @@ struct TreeNode {
1010
struct TreeNode *right;
1111
};
1212

13+
static int count = 0;
14+
static int SYM_NULL = INT_MIN;
15+
1316
static void ser(struct TreeNode *root, char **str, int *len) {
1417
if (root == NULL) {
15-
(*str)[(*len)++] = '#';
16-
(*str)[(*len)++] = ',';
18+
memcpy(str + *len, &SYM_NULL, sizeof(int));
19+
(*len) += sizeof(int);
1720
} else {
18-
int v = root->val;
19-
if (v < 0) {
20-
v = -v;
21-
(*str)[(*len)++] = '-';
22-
}
23-
while (v > 0) {
24-
(*str)[(*len)++] = v % 10 + '0';
25-
v /= 10;
26-
}
27-
(*str)[(*len)++] = ',';
21+
memcpy(str + *len, &root->val, sizeof(int));
22+
(*len) += sizeof(int);
2823
ser(root->left, str, len);
2924
ser(root->right, str, len);
3025
}
26+
count++;
3127
}
3228

33-
static struct TreeNode *des(char **str) {
34-
if (**str == '\0') {
29+
static struct TreeNode *des(char **str)
30+
{
31+
if (count == 0) {
3532
return NULL;
3633
}
37-
if (**str == '#') {
38-
(*str)++;
39-
(*str)++;
34+
count--;
35+
36+
int value;
37+
memcpy(&value, *str, sizeof(int));
38+
(*str) += sizeof(int);
39+
if (value == SYM_NULL) {
4040
return NULL;
4141
}
42-
43-
int i;
44-
bool sign = false;
4542
struct TreeNode *node = malloc(sizeof(struct TreeNode));
46-
node->val = 0;
47-
if (**str == '-') {
48-
sign = true;
49-
(*str)++;
50-
}
51-
for (i = 1; **str != ','; i *= 10) {
52-
node->val += i * ((**str) - '0');
53-
(*str)++;
54-
}
55-
if (sign) {
56-
node->val = -node->val;
57-
}
58-
(*str)++;
43+
node->val = value;
5944
node->left = des(str);
6045
node->right = des(str);
6146
return node;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
12+
* };
13+
*/
14+
class Codec {
15+
public:
16+
17+
// Encodes a tree to a single string.
18+
string serialize(TreeNode* root) {
19+
string res;
20+
ser(root, res);
21+
return res;
22+
}
23+
24+
// Decodes your encoded data to tree.
25+
TreeNode* deserialize(string data) {
26+
int len = data.length();
27+
return des(data.c_str(), &len);
28+
}
29+
30+
private:
31+
void ser(TreeNode* root, string& res) {
32+
if (root == nullptr) {
33+
res.push_back((char) 0x80);
34+
res.push_back((char) 0x00);
35+
res.push_back((char) 0x00);
36+
res.push_back((char) 0x00);
37+
return;
38+
}
39+
40+
ser(root->left, res);
41+
ser(root->right, res);
42+
43+
for (int i = sizeof(int) - 1; i >= 0; i--) {
44+
res.push_back(((char *)&root->val)[i]);
45+
}
46+
}
47+
48+
TreeNode* des(const char *data, int *len) {
49+
if (*len == 0) {
50+
return nullptr;
51+
}
52+
53+
int value;
54+
const char *s = data + *len - 1;
55+
for (int i = 0; i < sizeof(int); i++) {
56+
((char *)&value)[i] = *s--;
57+
}
58+
if (value == INT_MIN) {
59+
(*len) -= sizeof(int);
60+
return nullptr;
61+
}
62+
(*len) -= sizeof(int);
63+
64+
TreeNode *root = new TreeNode(value);
65+
root->right = des(data, len);
66+
root->left = des(data, len);
67+
return root;
68+
}
69+
};
70+
71+
// Your Codec object will be instantiated and called as such:
72+
// Codec ser, deser;
73+
// TreeNode* ans = deser.deserialize(ser.serialize(root));

0 commit comments

Comments
 (0)