1119PAT Class A, default left subtree and default right subtree solution

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

Analysis core:

  1. How to divide the left and right subtrees: the
    first element in the first order is equal to the last element in the subsequent order, which is the root node of the tree, and the penultimate element in the subsequent order is the node of the right child of the root node. The position of the node in the preorder is the point of distinction between left and right subtrees.
  2. Understand why there is a situation where the answer is not unique:
    Insert picture description here
    According to Example 2, when the number of children of a node is not 2, its position may be the left child or the right child.
//右孩子:
#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;
}


The right subtree refers to the code of God Liu. After thinking about it, I wrote the default method for the left subtree:

//左孩子
#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;
}

Comes with a test point:
input:
6
1 2 4 7 3 5
7 4 2 5 3 1

out:
1 7 4 2 1 5 3

Guess you like

Origin blog.csdn.net/qq_43992949/article/details/108357995