zoukankan      html  css  js  c++  java
  • 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意:

    输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历。

    trick:

    当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结构体储存结点的序号(1~N),编号(代表它在完全二叉树上的位置)和值。PTA网站上可以用大小为31的数组存放结点,可能数据为一颗比较平衡的二叉树。

    AAAAAccepted code:

    /*
    30
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
    */

     1 #define HAVE_STRUCT_TIMESPEC
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 int a[37],b[37];
     5 typedef struct node{
     6     int data;
     7     node *lchild,*rchild;
     8 }tree;
     9 tree *build(int l,int r,int L,int R){
    10     if(l>r)
    11         return NULL;
    12     tree *temp=new tree();
    13     temp->data=a[r];
    14     int i;
    15     for(i=L;i<=R;++i)
    16         if(b[i]==a[r])
    17             break;
    18     temp->lchild=build(l,l+i-L-1,L,i-1);
    19     temp->rchild=build(l+i-L,r-1,i+1,R);
    20     return temp;
    21 }
    22 int main(){
    23     ios::sync_with_stdio(false);
    24     cin.tie(NULL);
    25     cout.tie(NULL);
    26     int n;
    27     cin>>n;
    28     for(int i=1;i<=n;++i)
    29         cin>>a[i];
    30     for(int i=1;i<=n;++i)
    31         cin>>b[i];
    32     tree *temp=build(1,n,1,n);
    33     queue<tree *>q;
    34     q.push(temp);
    35     int flag=0;
    36     while(!q.empty()){
    37         tree *now=q.front();
    38         q.pop();
    39         if(flag)
    40             cout<<" ";
    41         cout<<now->data;
    42         flag=1;
    43         if(now->lchild)
    44             q.push(now->lchild);
    45         if(now->rchild)
    46             q.push(now->rchild);
    47     }
    48     return 0;
    49 }
     1 #define HAVE_STRUCT_TIMESPEC
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 int pos[37],in[37];
     5 typedef struct lv{
     6     int index,num;
     7 };
     8 lv level[37];
     9 int cnt=0;
    10 void find_(int iroot,int istart,int iend,int index){
    11     if(istart>iend)
    12         return;
    13     level[++cnt].index=index;
    14     level[cnt].num=pos[iroot];
    15     int i=istart;
    16     while(in[i]!=pos[iroot])
    17         ++i;
    18     find_(iroot-1-iend+i,istart,i-1,2*index+1);
    19     find_(iroot-1,i+1,iend,2*index+2);
    20 }
    21 bool cmp(lv a,lv b){
    22     if(a.index!=b.index)
    23         return a.index<b.index;
    24 }
    25 int main(){
    26     int n;
    27     cin>>n;
    28     for(int i=0;i<n;++i)
    29         cin>>pos[i];
    30     for(int i=0;i<n;++i)
    31         cin>>in[i];
    32     find_(n-1,0,n-1,0);
    33     sort(level+1,level+1+n,cmp);
    34     for(int i=1;i<cnt;++i)
    35         cout<<level[i].num<<" ";
    36     cout<<level[cnt].num;
    37     return 0;
    38 }
    保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
  • 相关阅读:
    PPT幻灯片放映不显示备注,只让备注显示在自己屏幕上
    Available Date 相关
    App Store常用推广方法
    iPhone应用提交流程:如何将App程序发布到App Store-转
    [转]关于适配iphone5,Invalid Launch Image的退信
    NSLog 不打印中文 - 解决
    Xcode GDB 调试
    xcode找不到真机设备 - 转
    使用静态库的一些问题 -all_load
    [深入浅出iOS库]之图形库CorePlot
  • 原文地址:https://www.cnblogs.com/ldudxy/p/11353950.html
Copyright © 2011-2022 走看看