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. Android(java)学习笔记76:多线程-定时器概述和使用

    定时器: 定时器是一个应用十分广泛的线程工具,可用于调度多个定时任务以后台线程的方式执行. 在Java中,可以通过Timer和TimerTask类来实现定义调度的功能 Timer public Tim ...

  2. thymleaf分支用法

    <div th:switch="${user.role}"> <p th:case="'admin'">User is an admin ...

  3. python(1) - 列表和元组

    列表(list) 列表是经常用到的一种数据类型,是一组有序的数据集合,可以将各种数据有序的存放在列表中,并且可以对其进行增删改查,以及遍历. 列表就是为了使变量能够存储更多的信息,比如我想存储一张购物 ...

  4. maya 2015配置openCollada插件

    1.下载对应的openCollada插件内容 https://github.com/KhronosGroup/OpenCOLLADA/wiki/OpenCOLLADA-Tools 该页面目前提供Mac ...

  5. [改善Java代码]警惕数组的浅拷贝

    建议62:警惕数组的浅拷贝 一.分析  在日常工作中,我们会遇见很多数组的拷贝和复制的问题,但是在你使用系统提供的API进行编码的时候,无形中会留下浅拷贝的隐患. 二.场景  有这样一个例子,第一个箱 ...

  6. PHP之自定义会话控制---使用文件处理

    前三篇简单的总结了下会话控制和文件操作,这一篇说说会话控制的自定义处理方式.既然知道了文件的基本读写,而且在会话控制中,也有人提到,session数据可以保存到缓存或数据库中,实际上当然不会是直接利用 ...

  7. JS中的嵌套作用域

    在JS中仅仅区分全局变量和局部变量还不够,实际上,变量作用域可以有任意层级(嵌套).其他函数内部定义的函数可以调用父函数的局部变量,而内部函数里定义的函数则不仅可以调用父函数的局部变量,还可以调用祖父 ...

  8. Redis 命令 - Sets

    SADD key member [member ...] Add one or more members to a set 127.0.0.1:6379> SADD foo hello (int ...

  9. Unity3D 之UGUI 滚动条

    先上效果图. 这里来说明下UGUI 滚动条,不涉及到代码. 主要用到的控件Scroll Rect ,Mask,Scrollbar. 第一步,建立一个Image,然后绑定一个滑动块的组件,添加一个mas ...

  10. 编译mosquitto出现的问题

    [root@localhost mosquitto-1.3]# make WITH_TLS=no set -e; for d in lib client src; do make -C ${d}; d ...