首页 > 学院 > 开发设计 > 正文

sdutacm-迷失の搜索树

2019-11-06 06:21:27
字体:
来源:转载
供稿:网友

迷失の搜索树

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

PRoblem Description

小璐在机缘巧合之下获得了一个二叉搜索树,这个二叉搜索树恰好有n个节点,每个节点有一个权值,每个节点的权值都在[1,n]这个区间内,并且两两不相同,真是优美的性质啊

但是命运的不公又让她失去了这个二叉搜索树

幸运的是,她还记得自己丢失的二叉搜索树的前序遍历序列。

在丢了二叉搜索树之后,小璐无比想念她的这个树的后序遍历

那么问题来了,聪明的你在知道这个二叉搜索树的前序遍历的序列的情况下,能帮她找到这个二叉搜索树的后序遍历嘛?

Input

 多组输入,以文件结尾

每组数据第一行为一个整数n,代表这个二叉搜索树的节点个数(1<=n<=100)

接下来一行n个整数,代表这个二叉搜索树的前序遍历序列

Output

输出n个整数

表示这个二叉树的后序遍历序列

Example Input

5

4 2 1 35

Example Output

1 3 2 54

Hint

 二叉查找树是一棵空树,或者是具有下列性质的二叉树:

若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值

若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值

它的左、右子树也分别为二叉排序树

Author

2016暑假集训结训赛 by QAQ

#include<stdio.h>#include<algorithm>#include<string.h>#include<iostream>#include<math.h>#include<queue>#include<stdlib.h>using namespace std;typedef struct node{   int data;   struct node *l;   struct node *r;}tree;char c[1002],d[1002],cc[1002];int k,kk,kkk;void insert(tree*&root,int x){     if(root == NULL)     {        tree*p;        p = (tree*)malloc(sizeof(tree));        p->data = x;        p->l = NULL;        p->r = NULL;        root =  p;     }     else if(x<root->data)     {         insert(root->l,x);     }     else     {        insert(root->r,x);     }}tree* build(tree*root,int a[],int o){    root = NULL;    for(int i= 0;i< o;i++)    {       insert(root,a[i]);    }    return root;}int top;//控制空格输出void inout(tree*root){     if(root)     {         inout(root->l);         /*if(top)top=0;         else printf(" ");*/         printf("%d",root->data);         inout(root->r);     }}void firstout(tree*root){    if(root)    {       firstout(root->l);       c[k++] = root->data;       firstout(root->r);    }}void lastout(tree*root){    if(root)    {       firstout(root->l);       firstout(root->r);       //cc[kkk++] = root->data;       if(top)top=0;       else printf(" ");       printf("%d",root->data);    }}void firstout2(tree*root){    if(root)    {       firstout(root->l);       d[kk++] = root->data;       firstout(root->r);    }}void qiangge(tree*root,tree* root2)//(比较两个已建成的二叉排序树是否相等){    if(root&&root2)    {       if(root->data!=root2->data)top = 0;       else       {          qiangge(root->l,root2->l);          qiangge(root->r,root2->r);       }    }    else if(root&&!root2||!root&&root2)top = 0;}void qlast(int *a,int *b,int len)//(根据前序中序求后序遍历){    if(len==0)return ;    int t = *a;    int i= 0;    for(;i<len;i++)    if(b[i]==*a)break;    qlast(a+1,b,i);    qlast(a+i+1,b+i+1,len-i-1);    if(top)top=0;    else printf(" ");    printf("%d",t);}int main(){    int n,o,oo,l;    while(~scanf("%d",&n))    {        top = 1;        int a[1002],b[1002],i,k;        for(i=0;i<n;i++)        {         scanf("%d",&a[i]);         b[i] = a[i];        }        for(i=0;i<n-1;i++)//将前序遍历冒泡排序后,即可得到该二叉树的中序遍历序列        for(k=0;k<n-i-1;k++)        if(b[k]>b[k+1])        swap(b[k],b[k+1]);        qlast(a,b,n);        printf("/n");    }   return 0;}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 8msTake Memory: 172KBSubmit time: 2017-02-08 10:31:40****************************************************/

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表