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

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

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

发表评论

访客

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