-
Notifications
You must be signed in to change notification settings - Fork 0
/
offer62.cpp
149 lines (133 loc) · 3.02 KB
/
offer62.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
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
/*****************************************************************************************************
* 剑指offer第62题
* 请实现两个函数,分别用来序列化和反序列化二叉树
* Input: 二叉树
* Output: 二叉树序列化和反序列换
*
* Note:
* 本题不做过多阐述,不太清楚题意是啥意思,建议多做leetcode 105和106
具体参考:
链接:https://www.nowcoder.com/questionTerminal/cf7e25aa97c04cc1a68c8f040e71fb84
1. 对于序列化:使用前序遍历,递归的将二叉树的值转化为字符,并且在每次二叉树的结点
不为空时,在转化val所得的字符之后添加一个 ',' 作为分割。对于空节点则以 '#' 代替。
2. 对于反序列化:按照前序顺序,递归的使用字符串中的字符创建一个二叉树(特别注意:
在递归时,递归函数的参数一定要是char *& ,这样才能保证每次递归后指向字符串的指针会
随着递归的进行而移动!!!)
* author: [email protected]
* time: 2019.7.7
******************************************************************************************************/
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
#define MAX 12
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
struct TreeNode *next;
//TreeNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
//}
};
//创建树
void CreateTree(TreeNode **root, int n)
{
if (n <= 0)
return;
TreeNode* arr[MAX];
for (int i = 0; i < n; i++)
{
arr[i] = new TreeNode;
arr[i]->left = NULL;
arr[i]->right = NULL;
}
int left, right;
for (int i = 0; i < n; i++)
{
cin >> left >> right;
if (left != -1)
arr[i]->left = arr[left - 1];
if (right != -1)
arr[i]->right = arr[right - 1];
}
*root = arr[0];
}
//删除树
void DeleteTree(TreeNode **root)
{
if ((*root) == NULL)
return;
DeleteTree(&((*root)->left));
DeleteTree(&((*root)->right));
delete *root;
}
//递归调用完成二叉树的序列化
void Serialize(TreeNode* root, string& str) {
if (root == NULL)
{
str += '#';
return;
}
string r = to_string(root->val);
str += r;
str += '\0';
Serialize(root->left, str);
Serialize(root->right, str);
}
char* Serialize(TreeNode* root)
{
if (root == NULL)
return NULL;
string str;
Serialize(root, str);
char *ret = new char[str.length() + 1];
int i;
for (i = 0; i < str.length(); i++)
{
ret[i] = str[i];
}
ret[i] = '\0';
return ret;
}
//*************************反序列化***********************
//递归调用完成二叉树的反序列化
TreeNode* aDeserialize(char *&str)
{
if (*str == '#')
{
str++;
return NULL;
}
int num = 0;
while (*str != '\0'&&*str != ',')
{
num = num * 10 + ((*str) - '\0');
(str)++;
}
TreeNode* root = new TreeNode(num);
if (*str == '\0')
return root;
else
(str)++;
root->left = aDeserialize(str);
root->right = aDeserialize(str);
}
TreeNode* Deserialize(char *str)
{
if (str == NULL)
return NULL;
TreeNode* ret = aDeserialize(str);
return ret;
}
int main()
{
int n;
while (cin >> n)
{
TreeNode *root = NULL;
CreateTree(&root, n);
Serialize(root);
DeleteTree(&root);
}
return 0;
}