Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7

2 3 1 5 7 6 4

1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

见之前一篇日志《序列建树(递归) 》

#include <iostream>

using namespace std;

struct LNode

{

  LNode *lchild,*rchild;

  int data;

};

int in[];

int pos[];

LNode* fun(int pos[],int f1,int r1,int in[],int f2,int r2)//生成树

{

      if(f1>r1)  return NULL;

      LNode *p=(LNode*)malloc(sizeof(LNode));

      p->lchild=NULL;

      p->rchild=NULL;

      p->data=pos[r1];

      int i;

      for(i=f2;i<=r2;i++)

            if(in[i]==pos[r1])  break;

      p->lchild=fun(pos,f1,f1+i-f2-,in,f2,i-);

      p->rchild=fun(pos,f1+i-f2,r1-,in,i+,r2);

     return p;     

}

int main()

{

      int n;

      while(cin>>n)

      {

            int i;

          for(i=;i<n;i++)

                  cin>>pos[i];

            for(i=;i<n;i++)

                  cin>>in[i];

            LNode *q=(LNode*)malloc(sizeof(LNode));

        q=fun(pos,,n-,in,,n-);

            LNode* que[];  //层次遍历

            int front=;int rear=;

            rear++;

            que[rear]=q;

        bool fir=true;

            while(front!=rear)

            {

               front++;

               if(fir)

               {cout<<que[front]->data;fir=false;}

               else cout<<" "<<que[front]->data;

               if(que[front]->lchild!=NULL)

                 que[++rear]=que[front]->lchild;

               if(que[front]->rchild!=NULL)

                 que[++rear]=que[front]->rchild; 

            }

            cout<<endl;

      }

   return ;

}

1020. Tree Traversals (序列建树)的更多相关文章

  1. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  2. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  3. 1020 Tree Traversals——PAT甲级真题

    1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...

  4. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  6. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  7. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  8. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

  9. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

随机推荐

  1. 两个数据库表同步的可视化WEB同步程序

    因业务升级,现有一个数据库中的表需要与先前项目中的表进行数据同步,停用先前的表,这两个表只能按其中相同的一个字段同步,认真研究了一下,用WEB程序进行了处理,可视化显示处理进度,同步操作结果.使用到的 ...

  2. Visual studio 2012 添加 GitHub

    文件-新建项目

  3. Mysql 5.6 新特性(转载)

    本文转载自 http://blog.csdn.net/wulantian/article/details/29593803 感谢主人的辛苦整理 一,安全提高 1.提供保存加密认证信息的方法,使用.my ...

  4. hdu 4606 简单计算几何+floyd+最小路径覆盖

    思路:将所有的直线的两个端点和城市混在一起,将能直接到达的两个点连线,求一次floyd最短路径.二分枚举bag容量,然后按给的要先后占领的城市由前向后,把能到一步到达的建一条边.然后求一次最小路径覆盖 ...

  5. server 2003上为单点登录sso配置映射

    单点登录不是本人做的,代码需要调用类似 http://***.com/login.sso 的地址.要成功调用,需要在IIS设置.sso为后缀的映射项. Win7系统下一设置完,就能成功调用. 但是服务 ...

  6. BZOJ 3831

    3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 121  Solved: 68[Submit][S ...

  7. Jersey(1.19.1) - Life-cycle of Root Resource Classes

    By default the life-cycle of root resource classes is per-request, namely that a new instance of a r ...

  8. 【Knockout】四、绑定上下文

    Binding context binding context是一个保存数据的对象,你可以在你的绑定中引用它.当应用绑定的时候,knockout自动创建和管理binding context的继承关系. ...

  9. 使用zipalign.exe工具优化APK安装包

    zipalign.exe是Android自带的一个档案整理工具,它可以用于优化APK安装包,从而提升Android应用与系统之间的交互效率,提升应用程序的运行速度. 操作步骤: 1.在命令行窗口输入如 ...

  10. SQL_从星期一到星期六自动打卡SQL代码

    create proc sp_MarkAutoKQ as begin ) ---创建两个变量,接收当前时间和当天是星期几 set @dateA=getdate() ---获取当前时间 set @dat ...