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. Storm中并发程度的理解

    Storm中涉及到了很多组件,例如nimbus,supervisor等等,在参考了这两篇文章之后,对这个有了更好的理解. Understanding the parallelism of a Stor ...

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

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

  3. CSS理解之padding--非原创

    因为在慕课网观看了张大神的视频,学习到了一点东西,向和大家分享.视频地址 代码如下: <!DOCTYPE html> <html lang="zh-CN"> ...

  4. MySQL数据库中的触发器

    --触发器是一类特殊的监控增删改操作,并产生相应的增删改的操作 --1,监视谁 2,监视动作 3,监视时间(之前或之后) 4,触发的事件 --触发器的简单语法 create trigger 触发器名字 ...

  5. Android 线程Thread的2种实现方法

    在讲解之前有以下三点要说明: 1.在Android中有两种实现线程Thread的方法: ①扩展java.long.Thread类: ②实现Runnable()接口: 2.Thread类是线程类,它有两 ...

  6. Objective-C中一个方法如何传递多个参数的理解

    原来如此 Objective-C语法中多参数传递方法经常是初学者最容易犯困的地方.我自己也是刚刚悟出来与大家分享. 分析 由于我们已有的语言经验告诉我们定义方法都是: 一个类型匹配一个参数(动态语言甚 ...

  7. 【Android】ADB常用指令与logcat日志(转)

    ADB命令简介 ADB是一个功能强大的命令行工具.通过它可以直接和模拟器或真机进行交互.它是一个具有客户端和服务器端的程序. 它主要由三个部分组成: 客户端,它运行在你的开发机上,你可以通过执行adb ...

  8. sql常识-union

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...

  9. 制作BibTex文件

    上一篇日志中讲到了在LaTeX中使用BibTex管理参考文献,这篇日志具体总结下如何制作BibTex文件. 制作BibTex文件,主要有以下几种方法: 手工制作: 直接从期刊数据库中下载: 借助Goo ...

  10. 第二十九篇、CoreAnimation的使用

    使用的的三个步骤 1.初始化演员 2.设置好剧情 3.播放 主要类: CALayer // 绘图部分 CABaseAnimation // 基本动画(缩放,移动) CAKeyframeAnimatio ...