PTA Playing Binary Tree (25 points)

It is the human mind that releases infinite light, and it is also the human mind that creates boundless darkness. Light and darkness are intertwined and fight together. This is the world for which we are nostalgic and helpless.

Given the middle-order traversal and pre-order traversal of a binary tree, please make a mirror inversion of the tree first, and then output the sequence of the inverted layer order traversal. The so-called mirror inversion refers to swapping the left and right children of all non-leaf nodes. It is assumed that the key values ​​are all positive integers that are not equal to each other.

Input format:

Enter the first line to give a positive integer N(≤30), which is the number of nodes in the binary tree. The second line gives the in-order traversal sequence. The third line gives its preorder traversal sequence. The numbers are separated by spaces.

Output format:

Output the sequence traversed by the tree inverted in one line. The numbers are separated by a space, and there must be no extra spaces at the beginning and end of the line.

Input sample:

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

Sample output:

4 6 1 7 5 3 2
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=1e4+10;
int N,zx[40],qx[40];
typedef struct node{
    int data;
    node *l,*r;
}kj,*tree;
void jtree(tree &t,int ks,int js,int cd){
    int ls;
    if(cd<=0){
        t=NULL;
        return ;
    }
    else{
        t=(tree)malloc(sizeof(kj));
        t->data=qx[ks];
        for(int i=0;i<N;i++){
            if(zx[i]==qx[ks]){
                ls=i;
                break;
            }
        }
        int cdf=ls-js;
        jtree(t->l,ks+1,js,cdf);
        int cdr=cd-1-cdf;
        jtree(t->r,ks+cdf+1,ls+1,cdr);
    }
    return ;
}
void fz(tree &t){
    if(t==NULL){
        return;
    }
    if(t->l!=NULL&&t->r!=NULL){
        tree temp;
        temp=t->l;
        t->l=t->r;
        t->r=temp;
    }
    else if(t->l==NULL&&t->r!=NULL){
        t->l=t->r;
        t->r=NULL;
    }
    else if(t->l!=NULL&&t->r==NULL){
        t->r=t->l;
        t->l=NULL;
    }
    fz(t->l);
    fz(t->r);
    return;
}
void cx(tree t){
    queue<tree> q;
    if(t!=NULL)
        q.push(t);
    while(!q.empty()){
        tree temp=q.front();
        cout<<temp->data;
        q.pop();
        if(temp->l!=NULL)
            q.push(temp->l);
        if(temp->r!=NULL)
            q.push(temp->r);
        if(!q.empty())
            cout<<' ';
    }
}
int main(){
    tree t;
    cin>>N;
    for(int i=0;i<N;i++)
        cin>>zx[i];
    for(int i=0;i<N;i++)
        cin>>qx[i];
    jtree(t,0,0,N);//建树
    fz(t);//翻转
    cx(t);//层序遍历输出
    cout<<endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44170305/article/details/108563425