程序填空题:Build Tree from Inorder and Postorder Traversals
The function `BuildTree` is to build and return a binary tree from its inorder and postorder 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 post[], int N )
{ //in[] stores the inorder traversal sequence
//and post[] stores the postorder 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 = @@[post[N-1]](3);
for (i=0; i if (in[i]==T->Data) break;
T->Left = BuildTree(@@[in, post, i](3));
T->Right = BuildTree(@@[in+i+1, post+i, N-i-1](3));
return T;
}
```
答案:
第1空:post[N-1]
第2空:in, post, i
第3空:in+i+1, post+i, 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 post[], int N )
{ //in[] stores the inorder traversal sequence
//and post[] stores the postorder 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 = @@[post[N-1]](3);
for (i=0; i
T->Left = BuildTree(@@[in, post, i](3));
T->Right = BuildTree(@@[in+i+1, post+i, N-i-1](3));
return T;
}
```
答案:
第1空:post[N-1]
第2空:in, post, i
第3空:in+i+1, post+i, N-i-1