|
|
@ -23,6 +23,7 @@ typedef struct TreeNode {
|
|
|
|
struct TreeNode *right; // 右子节点指针
|
|
|
|
struct TreeNode *right; // 右子节点指针
|
|
|
|
} TreeNode;
|
|
|
|
} TreeNode;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 构造函数 */
|
|
|
|
TreeNode *newTreeNode(int val) {
|
|
|
|
TreeNode *newTreeNode(int val) {
|
|
|
|
TreeNode *node;
|
|
|
|
TreeNode *node;
|
|
|
|
|
|
|
|
|
|
|
@ -34,93 +35,71 @@ TreeNode *newTreeNode(int val) {
|
|
|
|
return node;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Generate a binary tree with an array */
|
|
|
|
// 序列化编码规则请参考:
|
|
|
|
TreeNode *arrToTree(const int *arr, size_t size) {
|
|
|
|
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
|
|
|
if (size <= 0) {
|
|
|
|
// 二叉树的数组表示:
|
|
|
|
return NULL;
|
|
|
|
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
|
|
|
}
|
|
|
|
// 二叉树的链表表示:
|
|
|
|
|
|
|
|
// /——— 15
|
|
|
|
int front, rear, index;
|
|
|
|
// /——— 7
|
|
|
|
TreeNode *root, *node;
|
|
|
|
// /——— 3
|
|
|
|
TreeNode **queue;
|
|
|
|
// | \——— 6
|
|
|
|
|
|
|
|
// | \——— 12
|
|
|
|
|
|
|
|
// ——— 1
|
|
|
|
|
|
|
|
// \——— 2
|
|
|
|
|
|
|
|
// | /——— 9
|
|
|
|
|
|
|
|
// \——— 4
|
|
|
|
|
|
|
|
// \——— 8
|
|
|
|
|
|
|
|
|
|
|
|
/* 根节点 */
|
|
|
|
/* 将列表反序列化为二叉树:递归 */
|
|
|
|
root = newTreeNode(arr[0]);
|
|
|
|
TreeNode *arrayToTreeDFS(int *arr, int size, int i) {
|
|
|
|
/* 辅助队列 */
|
|
|
|
if (i < 0 || i >= size || arr[i] == INT_MAX) {
|
|
|
|
queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);
|
|
|
|
return NULL;
|
|
|
|
// 队列指针
|
|
|
|
|
|
|
|
front = 0, rear = 0;
|
|
|
|
|
|
|
|
// 将根节点放入队尾
|
|
|
|
|
|
|
|
queue[rear++] = root;
|
|
|
|
|
|
|
|
// 记录遍历数组的索引
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
while (front < rear) {
|
|
|
|
|
|
|
|
// 取队列中的头节点,并让头节点出队
|
|
|
|
|
|
|
|
node = queue[front++];
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (index < size) {
|
|
|
|
|
|
|
|
// represent null with INT_MAX
|
|
|
|
|
|
|
|
if (arr[index] != INT_MAX) {
|
|
|
|
|
|
|
|
node->left = newTreeNode(arr[index]);
|
|
|
|
|
|
|
|
queue[rear++] = node->left;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if (index < size) {
|
|
|
|
|
|
|
|
if (arr[index] != INT_MAX) {
|
|
|
|
|
|
|
|
node->right = newTreeNode(arr[index]);
|
|
|
|
|
|
|
|
queue[rear++] = node->right;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 释放辅助队列空间
|
|
|
|
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
|
|
|
|
free(queue);
|
|
|
|
root->val = arr[i];
|
|
|
|
|
|
|
|
root->left = arrayToTreeDFS(arr, size, 2 * i + 1);
|
|
|
|
|
|
|
|
root->right = arrayToTreeDFS(arr, size, 2 * i + 2);
|
|
|
|
return root;
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Generate a binary tree with an array */
|
|
|
|
/* 将列表反序列化为二叉树 */
|
|
|
|
int *treeToArr(TreeNode *root) {
|
|
|
|
TreeNode *arrayToTree(int *arr, int size) {
|
|
|
|
|
|
|
|
return arrayToTreeDFS(arr, size, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 将二叉树序列化为列表:递归 */
|
|
|
|
|
|
|
|
void treeToArrayDFS(TreeNode *root, int i, int *res, int *size) {
|
|
|
|
if (root == NULL) {
|
|
|
|
if (root == NULL) {
|
|
|
|
return NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int front, rear;
|
|
|
|
while (i >= *size) {
|
|
|
|
int index, *arr;
|
|
|
|
res = realloc(res, (*size + 1) * sizeof(int));
|
|
|
|
TreeNode *node;
|
|
|
|
res[*size] = INT_MAX;
|
|
|
|
TreeNode **queue;
|
|
|
|
(*size)++;
|
|
|
|
/* 辅助队列 */
|
|
|
|
|
|
|
|
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
|
|
|
|
|
|
|
// 队列指针
|
|
|
|
|
|
|
|
front = 0, rear = 0;
|
|
|
|
|
|
|
|
// 将根节点放入队尾
|
|
|
|
|
|
|
|
queue[rear++] = root;
|
|
|
|
|
|
|
|
/* 辅助数组 */
|
|
|
|
|
|
|
|
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
|
|
|
|
|
|
|
// 数组指针
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
while (front < rear) {
|
|
|
|
|
|
|
|
// 取队列中的头节点,并让头节点出队
|
|
|
|
|
|
|
|
node = queue[front++];
|
|
|
|
|
|
|
|
if (node != NULL) {
|
|
|
|
|
|
|
|
arr[index] = node->val;
|
|
|
|
|
|
|
|
queue[rear++] = node->left;
|
|
|
|
|
|
|
|
queue[rear++] = node->right;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
arr[index] = INT_MAX;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
res[i] = root->val;
|
|
|
|
|
|
|
|
treeToArrayDFS(root->left, 2 * i + 1, res, size);
|
|
|
|
|
|
|
|
treeToArrayDFS(root->right, 2 * i + 2, res, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Free the memory allocated to a tree */
|
|
|
|
/* 将二叉树序列化为列表 */
|
|
|
|
|
|
|
|
int *treeToArray(TreeNode *root, int *size) {
|
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
int *res = NULL;
|
|
|
|
|
|
|
|
treeToArrayDFS(root, 0, res, size);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 释放二叉树内存 */
|
|
|
|
void freeMemoryTree(TreeNode *root) {
|
|
|
|
void freeMemoryTree(TreeNode *root) {
|
|
|
|
if (root == NULL)
|
|
|
|
if (root == NULL)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
freeMemoryTree(root->left);
|
|
|
|
freeMemoryTree(root->left);
|
|
|
|
freeMemoryTree(root->right);
|
|
|
|
freeMemoryTree(root->right);
|
|
|
|
// 释放内存
|
|
|
|
|
|
|
|
free(root);
|
|
|
|
free(root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|