-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Build Tree from Inorder and Postorder Traversals

Luz4年前 (2021-05-10)题库1087
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

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。