-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path297.serialize-and-deserialize-binary-tree.c
More file actions
154 lines (128 loc) · 3.81 KB
/
Copy path297.serialize-and-deserialize-binary-tree.c
File metadata and controls
154 lines (128 loc) · 3.81 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Definition for a binary tree node.
*/
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#define INIT_SIZE 999
#define GROW_MULTI 2
typedef struct string
{
char *str;
size_t length;
size_t capacity;
} string;
string *stringCreate()
{
string *s = (string *)malloc(sizeof(string));
if (s == NULL)
return NULL;
s->capacity = INIT_SIZE;
s->str = (char *)malloc(sizeof(char) * s->capacity);
if (s->str == NULL)
return NULL;
s->str[0] = '\0';
s->length = 0;
return s;
}
void stringAppend(string *s, const char *new)
{
if (s->length + strlen(new) + 1 > s->capacity)
{
char *news = (char *)realloc(s->str, sizeof(char) * (s->capacity * GROW_MULTI));
if (news == NULL)
return;
s->str = news;
s->capacity *= GROW_MULTI;
}
strcat(s->str, new);
s->length = strlen(s->str);
}
string *serializeHelper(struct TreeNode *root, string *str)
{
if (root == NULL)
{
stringAppend(str, "n ");
return str;
}
char temp[99];
sprintf(temp, "%d ", root->val);
stringAppend(str, temp);
str = serializeHelper(root->left, str);
str = serializeHelper(root->right, str);
return str;
}
/** Encodes a tree to a single string. */
char *serialize(struct TreeNode *root)
{
if (root == NULL)
return "";
string *s = stringCreate();
s = serializeHelper(root, s);
return s->str;
}
struct TreeNode *deserializeHelper(char **data)
{
if ((*data)[0] == 'n')
return NULL;
int val = atoi(*data);
struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = val;
*data = strchr(*data, ' ') + 1;
root->left = deserializeHelper(data);
*data = strchr(*data, ' ') + 1;
root->right = deserializeHelper(data);
return root;
}
/** Decodes your encoded data to tree. */
struct TreeNode *deserialize(char *data)
{
if (strlen(data) == 0)
return NULL;
return deserializeHelper(&data);
}
// Your functions will be called as such:
// char* data = serialize(root);
// deserialize(data);
int main(int argc, char const *argv[])
{
// string *s = stringCreate();
// printf("%zu\n", strlen(s->str));
// printf("%d\n", s->length);
// printf("%s length: %d capacity: %d\n", s->str, s->length, s->capacity);
// stringAppend(s, "a");
// printf("%s length: %d capacity: %d\n", s->str, s->length, s->capacity);
// stringAppend(s, "bcdefg");
// printf("%s length: %d capacity: %d\n", s->str, s->length, s->capacity);
// stringAppend(s, "hijklmnopqrstuvwxyz");
// printf("%s length: %d capacity: %d\n", s->str, s->length, s->capacity);
struct TreeNode *rootRightLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightLeft->val = 4;
rootRightLeft->left = rootRightLeft->right = NULL;
struct TreeNode *rootRightRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRightRight->val = 5;
rootRightRight->left = rootRightRight->right = NULL;
struct TreeNode *rootLeft = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootLeft->val = 2;
rootLeft->left = NULL;
rootLeft->right = NULL;
struct TreeNode *rootRight = (struct TreeNode *)malloc(sizeof(struct TreeNode));
rootRight->val = 3;
rootRight->left = rootRightLeft;
rootRight->right = rootRightRight;
struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = rootLeft;
root->right = rootRight;
char *data = serialize(root);
root = deserialize(data);
printf("%d %d %d\n", root->val, root->left->val, root->right->val);
printf("%d %d %d\n", root->val, root->right->left->val, root->right->right->val);
return 0;
}