[PAT] Grade 1119 Pre- and Post-order Traversals (preamble sequence after sequence revolution)

[Link] title

 

[Title] Italy

According to the preamble and the subsequent sequence of binary tree, if the sequence unique sequence, output Yes, if not unique output No, and outputs the sequence in the sequence.

 

【answer】

As we all know, it is not a binary tree can be established according to the preamble and in order, why? First need a clear sequence of preorder traversal order is: about root traversal sequence after sequence sequence is: about root.

Then we look for this sample (not in order to better illustrate the uniqueness of the subject is not given a sample):

Preamble: . 1 . 4. 3. 6 2. 5. 7

After the sequence: . 3. 6. 4 . 5. 7 2 . 1

First, a binary tree root must be whole grain, according to the preamble sequence and then we can know 4 subtree 1 (not sure at this time is still left subtree right subtree), then you can get 4 in a subsequent sequence position, since the subsequent sequence of traversal order is about the root, it can be easily determined 3 and 6 must be a child node 4, this time we know have a root node 4 has two child nodes (i.e. code NUM), and then to count the preamble sequence, we found prer-prel - 1 (that is, remove 14) is greater than found num, since the sequence preorder traversal around the root, so prer - prel -1 - num> 0 will not be described Well there are nodes removed than the left subtree, then we just 4 as the root of the tree is left subtree 1, 257 that is, the right subtree.

Then we continue the recursive (that is just the sequence into a left section (463 after the preamble sequence 364) and a right section (sequence 572 after the preamble 257)), and we look at the left child tree it, this is when you can know subtree 6 4 sequence in order to find the 6, it can be determined that the child node 6 3, then we can know a sub-node 6 has a root node, and then preamble sequence found in only one 3, this time we are not sure 6 is a left or right subtree 4 of the sub-tree.

[Code]

#include<bits/stdc++.h>
using namespace std;
const int maxn = 35;
int pre[maxn], post[maxn];
int f = 1;
vector<int>v;
void getorder(int prel, int prer, int postl, int postr)
{
    if (prel == prer)
    {
        v.push_back(pre[prel]);
        return;
    }
    int a = pre[prel + 1];
    int= postidx Postl;
     the while (POST [postidx] && postidx = a <= PostR!) postidx ++ ;
     int NUM = postidx - Postl; // number of nodes in a subtree of the root node 
    IF (PreR - Prel - . 1 == NUM) F = 0 ; 
    
    / * sequence in the previous sequence, if the pre [prel] is a root node of the subtree node is equal to the number of nodes -a num, 4 
    so long can not be determined in a left pre [prel] of subtree or right subtree, here, we assume a left subtree * / 
    getOrder (Prel + . 1 , Prel + + NUM . 1 , Postl, postidx); 
    
    v.push_back (pre [Prel]); 
    
    / * preamble sequence , if the pre [prel] is a root node of the subtree nodes of this node is greater than -a num, 
    because the preamble sequence is traversed around the root, then the description to pre [prel] as well as the root tree right subtree * / 
    IF (PreR - Prel -1> num)
        getorder(prel + num + 2, prer, postidx + 1, postr - 1);
}
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &pre[i]);
    for (int i = 0; i < n; i++)
        scanf("%d", &post[i]);
    getorder(0, n - 1, 0, n - 1);
    if(f)printf("Yes\n");
    else printf("No\n");
    printf("%d", v[0]);
    for (int i = 1; i < n; i++)
        printf(" %d", v[i]);
    printf("\n");
}

 

Guess you like

Origin www.cnblogs.com/z1014601153/p/11369941.html