HLG2040 two fork tree traversal known before, after seeking

Source: Internet
Author: User

Traversal of a binary tree
Time limit:1000 MS Memory limit:32768 K
Total submit:60(users) Total accepted:34(users) Rating: Special Judge: No
Description
A binary tree is given a middle order and a pre-sequence traversal, which outputs its post-order traversal.
Input

The subject has multiple sets of data, input processing to the end of the file.

The first row of each set of data includes an integer n, which indicates that the binary tree has n nodes in total.

The next line contains n integers for each row, indicating the middle sequence traversal of this tree.

The next line contains n integers for each row, indicating the first sequence traversal of the tree.

3<= N <= 100

Output
Each set of outputs includes a row that indicates the sequential traversal of this tree.
Sample Input
7
4 2 5 1 6 3 7

1 2 4 5 3 6 7

Sample Output
4 5 2 6 7 3 1
Source
Winter Holiday Contest 5

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace Std;

int n,num=0;
int KMP (int root,int str2[])
{
int s=0;
while (1)
{
if (str2[s]==root)
return s;
s++;
}
return 0;
}
void calculate (int m,int str1[],int str2[],int str3[])
{
if (m<=0)
return;
int LEN=KMP (STR1[0],STR2);
Calculate (LEN,STR1+1,STR2,STR3);
Calculate (M-LEN-1,STR1+LEN+1,STR2+LEN+1,STR3);
STR3[NUM++]=STR1[0];
}
int main ()
{
int n;
int str1[105],str2[105],str3[105];
while (scanf ("%d", &n)!=eof)
{
num=0;
memset (str1,0,sizeof (str1));
memset (str2,0,sizeof (str2));
memset (str3,0,sizeof (STR3));
for (int i=0;i<n;i++)
scanf ("%d", &str2[i]);
for (int i=0;i<n;i++)
scanf ("%d", &str1[i]);
Calculate (N,STR1,STR2,STR3);
for (int i=0;i<n;i++)
printf ("%d", str3[i]);
printf ("\ n");
}
return 0;
}

HLG2040 two fork tree traversal known before, after seeking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.