程序填空题:Build Tree from Inorder and Preorder Traversals
The function `BuildTree` is to build and return a binary tree from its inorder and preorder traversal sequences.
The tree structure is defined as the following:
```
typedef struct Node *PtrToNode;
struct Node{
int Data;
PtrToNode Left, Right;
};
typedef PtrToNode Tree;
```
Please fill in the blanks.
```
Tree BuildTree( int in[], int pre[], int N )
{ //in[] stores the inorder traversal sequence
//and pre[] stores the preorder traversal sequence
//N is the number of nodes in the tree
Tree T;
int i;
if (!N) {
return NULL;
}
T = (Tree)malloc(sizeof(struct Node));
T->Data = @@[pre[0]](3);
for (i=0; i if (in[i]==T->Data) break;
T->Left = BuildTree(@@[in, pre+1, i](3));
T->Right = BuildTree(@@[in+i+1, pre+i+1, N-i-1](3));
return T;
}
```
答案:
第1空:pre[0]
第2空:in, pre+1, i
第3空:in+i+1, pre+i+1, N-i-1
The tree structure is defined as the following:
```
typedef struct Node *PtrToNode;
struct Node{
int Data;
PtrToNode Left, Right;
};
typedef PtrToNode Tree;
```
Please fill in the blanks.
```
Tree BuildTree( int in[], int pre[], int N )
{ //in[] stores the inorder traversal sequence
//and pre[] stores the preorder traversal sequence
//N is the number of nodes in the tree
Tree T;
int i;
if (!N) {
return NULL;
}
T = (Tree)malloc(sizeof(struct Node));
T->Data = @@[pre[0]](3);
for (i=0; i
T->Left = BuildTree(@@[in, pre+1, i](3));
T->Right = BuildTree(@@[in+i+1, pre+i+1, N-i-1](3));
return T;
}
```
答案:
第1空:pre[0]
第2空:in, pre+1, i
第3空:in+i+1, pre+i+1, N-i-1