PAT 1020. Tree Traversals

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>
#include<vector>
using namespace std;
vector<int> post,in,level(100000,-1);
void pre(int root,int s,int e,int index){
if(s>e) return ;
level[index]=post[root];
int i=s;
for(;i<=e;i++)
if(in[i]==post[root]) break;
pre(root-(e-i+1),s,i-1,index*2+1);
pre(root-1,i+1,e,index*2+2);
}
int main(){
int N; cin>>N;
post.resize(N); in.resize(N);
for(int i=0;i<N;i++) cin>>post[i];
for(int i=0;i<N;i++) cin>>in[i];
pre(N-1,0,N-1,0);
for(int i=0;i<level.size();i++)
if(level[i]!=-1)
i>0?cout<<" "<<level[i]:cout<<level[i];
return 0;
}

PAT 1020. Tree Traversals的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. PAT Advanced 1020 Tree Traversals (25 分)

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

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

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

  8. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

  9. PAT 1086 Tree Traversals Again[中序转后序][难]

    1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...

随机推荐

  1. EasyUI Datagrid 分页显示(客户端)

    转自:https://blog.csdn.net/metal1/article/details/17536185 EasyUI Datagrid 分页显示(客户端) By ZYZ 在使用JQuery ...

  2. Java源码——String

    最近在研究java的源代码,但是由于自己英语水平有限,所以想使用中文注释的方式把源码里的方法全部重写 一遍,下面是楼主整理出来的一小部分.我把整体的项目托管到GitHub上了,欢迎大家前去交流学习. ...

  3. (数论)51NOD 1073 约瑟夫环

    N个人坐成一个圆环(编号为1 - N),从第1个人开始报数,数到K的人出列,后面的人重新从1开始报数.问最后剩下的人的编号.例如:N = 3,K = 2.2号先出列,然后是1号,最后剩下的是3号.In ...

  4. Rabbitmq笔记一

    几个基本概念 Producer 生产者,发送消息的一方,图中左侧的client. Consumer 消费者,接收消息的一方,图中后侧的client. Broker 消息中间件的服务节点,一般一个Rab ...

  5. Luogu P5027 【Barracuda】(高斯消元)

    祭一下第一道独立做出来的高斯消元(虽然在各大佬看来都是水题...) 首先这道题给了你n+1个一次方程,n个未知数 其中有一个方程是错误的 求解在合法的前提下最大的未知数是多少... 显然高斯消元... ...

  6. Java多线程(九) synchronized 锁对象的改变

    public class MyService { private String lock = "123"; public void testMethod() { synchroni ...

  7. 莫队算法/二分查找 FZU 2072 Count

    题目传送门 题意:问区间内x的出现的次数分析:莫队算法:用一个cnt记录x的次数就可以了.还有二分查找的方法 代码: #include <cstdio> #include <algo ...

  8. iOS 项目中的常见文件

    iOS的笔记-项目中的常见文件   新建一个项目之后,有那么多的文件,下面介绍一下主要的几个. 1.文件名 (1)AppDelegate UIApplication的代理,app收到干扰的时候,进行处 ...

  9. sql删除表中重复记录只保留一条记录

    最终代码 update T_Fee set gzl_dfg_op = 'delete' where MetReadRecordID in ( select MetReadRecordID from T ...

  10. 关于java的print()

    print方法是类PrintStream的方法成员,而System类有一个static的PrintStream类型的属性成员,名叫out,我们平时写的System.out.print("he ...