迷失の搜索樹
TimeLimit: 1000MS Memory Limit: 65536KB
SubmitStatistic
小璐在機緣巧合之下獲得了一個二叉搜索樹,這個二叉搜索樹恰好有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****************************************************/ 
新聞熱點
疑難解答