#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *left;
struct Node *right;
} Node;
Node *createNode(int data) {
if (data == -1) {
return NULL;
}
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
Node *buildTree(int *arr, int idx, int n) {
if (idx >= n || arr[idx] == -1) {
return NULL;
}
Node *root = createNode(arr[idx]);
root->left = buildTree(arr, 2 * idx + 1, n);
root->right = buildTree(arr, 2 * idx + 2, n);
return root;
}
void inorderTraversal(Node *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5, -1, 6, -1, -1, 7, 8, -1, -1, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
Node *root = buildTree(arr, 0, n);
printf("中序遍历结果:\n");
inorderTraversal(root);
return 0;
}