PAT-A1119 Pre- and Post-order Traversals 题目内容及题解

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

题目大意

题目给定一个二叉树的前序遍历和后序遍历,要求指出此二叉树是否唯一并输出其中序遍历。如果不唯一,则输出任意一个中序便利即可。

解题思路

  1. 读入前序遍历及后序遍历;
  2. 根据两个遍历生成二叉树,并标记其是否唯一(如果左右子树一边不存在内容则不唯一,将其假设为出现在左子树上);
  3. 中序遍历生成的二叉树并记录中序遍历;
  4. 根据题目要求输出结果并返回零值。

代码

#include<cstdio>
#define maxn 35
int pre[maxn],post[maxn],in[maxn];
int flag,N,seq;

struct Node{
    int data;
    struct Node *lchild,*rchild;
};



Node* Create(int preL,int preR,int postL,int postR){
    Node* root;
    int k;
    int num2=preR-preL,num1=0;
    if(preL>preR){
        return NULL;
    }
    root=new(Node);
    root->data=pre[preL];
    if(preL==preR){
        root->lchild=NULL;
        root->rchild=NULL;
        return root;
    }
    for(k=postL;k<=postR;k++){
        num1++;
        num2--;
        if(post[k]==pre[preL+1]){
            break;
        }
    }
    if(num1==0||num2==0){
        flag=1;
    }
    root->lchild=Create(preL+1,preL+num1,postL,postL+num1-1);
    root->rchild=Create(preL+num1+1,preR,postL+num1,postR-1);
    return root;
}

void InOrder(Node* root){
    if(root==NULL){
        return;
    }
    InOrder(root->lchild);
    in[seq++]=root->data;
    InOrder(root->rchild);
}

void Print(){
    int i;
    if(flag){
        printf("No\n");
    }else{
        printf("Yes\n");
    }
    for(i=0;i<N;i++){
        printf("%d",in[i]);
        if(i<N-1){
            printf(" ");
        }else{
            printf("\n");
        }
    }
}

int main(){
    int i;
    Node* root=NULL;
    scanf("%d",&N);
    for(i=0;i<N;i++){
        scanf("%d",&pre[i]);
    }
    for(i=0;i<N;i++){
        scanf("%d",&post[i]);
    }
    root=Create(0,N-1,0,N-1);
    InOrder(root);
    Print();
    return 0;
}

运行结果

PAT-A1119 Pre- and Post-order Traversals 题目内容及题解