1119PAT甲级,默认左子树和默认右子树解法

1119 Pre- and Post-order Traversals (30分)

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. 明白为什么会出现答案不唯一的情况:
    在这里插入图片描述
    根据样例二得出,当一个结点的孩子数目不是2个的时候,则它的位置可能是左孩子也可能是右孩子。
//右孩子:
#include <bits/stdc++.h>
using namespace std;
int n;
vector <int> pre,in,post;
bool flag = true;
void inorder(int prel,int prer,int posl,int posr){
    
    
    if(prel == prer){
    
    
        in.push_back(pre[prel]);
        return ;
    }
    if(pre[prel] == post[posr]){
    
    
        int k = prel + 1;
        while(k<=prer && post[posr-1] != pre[k]) k++;
        if(k-prel > 1){
    
    
            inorder(prel+1,k-1,posl,posl+(k-prel-1)-1);
        }else{
    
    
            flag = false; //则不需要对左子树进行遍历,直接进入右子树
        }
        in.push_back(post[posr]);
        inorder(k,prer,posl+(k-prel-1),posr-1);
    }
}
int main(){
    
    
    scanf("%d",&n);
    pre.resize(n);post.resize(n);
    for(int i=0;i<n;i++){
    
    
        scanf("%d",&pre[i]);
    }
    for(int i=0;i<n;i++){
    
    
        scanf("%d",&post[i]);
    }
    inorder(0,n-1,0,n-1);
    printf("%s\n",flag? "Yes" : "No");
    for(int i=0;i<n;i++){
    
    
        printf("%d%s",in[i],i==n-1? "\n" : " ");
    }
    return 0;
}


右子树是参考了柳神的code,自己有所思考后,写出了默认为左子树的方法:

//左孩子
#include <bits/stdc++.h>
using namespace std;
int n;
vector <int> pre,in,post;
bool flag = true;
void inorder(int prel,int prer,int posl,int posr){
    
    
    if(prel == prer){
    
    
        in.push_back(pre[prel]);
        return ;
    }
    if(pre[prel] == post[posr]){
    
    
        int k = prel + 1;
        while(k<=prer && post[posr-1] != pre[k]) k++;
        if(k-prel > 1){
    
    
            inorder(prel+1,k-1,posl,posl+(k-prel-1)-1);
            in.push_back(post[posr]);
            inorder(k,prer,posl+(k-prel-1),posr-1);
        }else{
    
    
            flag = false;
            inorder(prel+1,prer,posl,posr-1);//默认左子树,因此只需要递归调用后移一位的左子树
            in.push_back(pre[prel]);
        }
    }
}
int main(){
    
    
    scanf("%d",&n);
    pre.resize(n);post.resize(n);
    for(int i=0;i<n;i++){
    
    
        scanf("%d",&pre[i]);
    }
    for(int i=0;i<n;i++){
    
    
        scanf("%d",&post[i]);
    }
    inorder(0,n-1,0,n-1);
    printf("%s\n",flag? "Yes" : "No");
    for(int i=0;i<n;i++){
    
    
        printf("%d%s",in[i],i==n-1? "\n" : " ");
    }
    return 0;
}

附赠一个测试点:
input:
6
1 2 4 7 3 5
7 4 2 5 3 1

out:
1 7 4 2 1 5 3

猜你喜欢

转载自blog.csdn.net/qq_43992949/article/details/108357995