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. ie6双边距bug及其解决办法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. java.lang.ClassNotFoundException: com.servlet.HandlesearchclassesServlet

    错误的原因: 原来命名为Handlesearchclasses导致与系统预留字冲突, 导致servlet找不到,出现404错误, 因此该变类名和web.xml里面的配置文件即可.

  3. hibernate 创建session

    //1. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = null; //1). 创建 Configuration 对象: 对应 hibe ...

  4. 神奇的CSS3选择器

    话说园子里也混迹多年了,但是基本没写过blog,写点基础的,那就从css3选择器开始吧. Css3选择器 先说下,为什么提倡使用选择器. 使用选择器可以将样式与元素直接绑定起来,在样式表中什么样式与什 ...

  5. MyBatis(3.2.3) - Configuring MyBatis using XML, Settings

    The default MyBatis global settings, which can be overridden to better suit application-specific nee ...

  6. MongoDB - Introduction to MongoDB

    MongoDB is an open-source document database that provides high performance, high availability, and a ...

  7. Httpclient 和jsoup结和提取网页内容(某客学院视频链接)

    最近在极客学院获得体验会员3个月,然后就去上面看了看,感觉课程讲的还不错.整好最近学习Android,然后去上面找点视频看看.发现只有使用RMB买的会员才能在上面下载视频.抱着试一试的态度,去看他的网 ...

  8. windows7下系统保护中出现错误“文件名、目录名或卷标语法不正确。(0x8007007B)“ 以及保护设置列表中出现“Windows7_os(c:)(找不到)”选项时的解决方法

    windows7下系统保护功能很是鸡肋,有事会出现一下两个问题: 1.出现错误“文件名.目录名或卷标语法不正确.(0x8007007B) 2.保护设置列表中出现“Windows7_os(c:)(找不到 ...

  9. 实例介绍Cocos2d-x物理引擎:使用关节

    在游戏中我们可以通过关节约束两个物体的运动.我们通过一个距离关节实例,介绍一下如何在使用关节. 这个实例的运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点和附近生成 ...

  10. ICallbackEventHandler 接口实现回调处理功能

    在最近的项目实现中遇到了一个问题 在数据处理的过程中,需要请求获取数据,再做处理之后,可以在页面及时获取数据 开始时,首先想到的到是写Ajax请求,但在做后续数据处理后,处理获取数据等操作,感觉实现起 ...