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. Nodejs的mysql模块学习(一)

    介绍 mysql npm包 是一个nodejs的模块,由JavaScript编写 安装 npm install mysql 建立连接 var mysql = require('mysql');//引用 ...

  2. 杂乱无章之javascript(一)

    1.in 要求第一个(左边的)操作数必须是字符串类型或是可以转化成字符串类型的其他类型,而第二(右边的)操作数必须是数组或对象.只有第一个操作数的值是第二个操作数的属性名,才会返回true,否则返回f ...

  3. 使用SCNetworkReachability判断网络是否连接

    先来看一下整个方法 - (BOOL)isConnectionAvailable { //创建零地址,0.0.0.0的地址表示查询本机的网络连接状态 struct sockaddr_in zeroAdd ...

  4. ArcGIS Server 10.2 实战(一)Asp.net MVC与JSON数据妙用实现动态生成要素图层

    今年7月刚刚发布的ArcGIS 10.2为GIS的web开发带来了一个很实在的功能,JSON转要素.以往GIS图层外部数据(如文本数据,数据库数据)动态地写入地图服务中的图层是一件不可想象的事情,如今 ...

  5. javaweb学习总结十二(JAXP对XML文档进行SAX解析)

    一:JAXP使用SAX方式解析XML文件 1:dom解析与sax解析异同点 2:sax解析特点 二:代码案例 1:xml文件 <?xml version="1.0" enco ...

  6. H.264编码之IDCT变换原理

    上次讲到了DCT变换的推导过程,这次主要给大家讲下IDCT反变换的推导过程.建议大家先看上次讲的DCT变换公式推导过程.这样在看这篇文章时会容易很多!话不多说,让我们开始IDCT的旅程吧! IDCT反 ...

  7. [改善Java代码]枚举和注解结合使用威力更大

    注解的写法和接口很类似,都采用了关键字interface,而且都不能有实现代码,常量定义默认都是pulbic static final类型的. 他们的主要不同点是:注解在interface前加上@字符 ...

  8. JVM 垃圾回收 Minor gc vs Major gc vs Full gc

    关于垃圾回收机制及比较请参见:http://colobu.com/2015/04/07/minor-gc-vs-major-gc-vs-full-gc/ http://colobu.com/2014/ ...

  9. 用java调用oracle存储过程总结(转)

    //1.call+包名+存储过程名(传入.传出值用?) String str="{call SMSBUSINESS.deleteZhZMember(?,?,?)}"; //2.建立 ...

  10. 刚更新的css hack技巧

    一 一般Hack 1概念: 不同的浏览器对CSS的解析效果不同,为了达到相同的效果,就得根据不同浏览器写不同的css 2规则: CSS Hack大致有3种表现形式,CSS类内部Hack.选择器Hack ...