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. 基于JWT机制的单点登录

    使用JWT实现单点登录时,需要注意token时效性.token是保存在客户端的令牌数据,如果永久有效,则有被劫持的可能.token在设计的时候,可以考虑一次性有效或一段时间内有效.如果设置有效时长,则 ...

  2. VS2015 framework4.5代码提示英文切换为中文

    输入下面的地址,复制里面所有的文件 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 ...

  3. poj 1286 Necklace of Beads【polya定理+burnside引理】

    和poj 2409差不多,就是k变成3了,详见 还有不一样的地方是记得特判n==0的情况不然会RE #include<iostream> #include<cstdio> us ...

  4. 清北考前刷题day6下午好

    /* 贪心 负数一定不取 枚举最高位是1 且答案取为0的 位置, 更新答案. */ #include<iostream> #include<cstdio> #include&l ...

  5. js截取字符串 区分中英文

    方法如下: //在一个字符串中截取前面部分文字,汉字.全角符号按2个占位,数字英文.半角按一个占位,未显示完的最后加入“……”. //适合多行显示. function suolve(str, sub_ ...

  6. jvm内存分区

    java内存是由jvm进行管理的,其内存简易模型如下图: jvm管理的内存大体上可分为方法区.堆.程序计数器.线程栈.本地方法区这几部分.方法区:主要存放类的元信息(包括类的名称.修饰符.静态变量.f ...

  7. 计算给定数组 arr 中所有元素的总和的几种方法

    1.forEach遍历: function sum(arr) {     var result = 0;     arr.forEach(function(item,index) {          ...

  8. 关于 VS 调用存储过程加载很慢和SQL 执行很快的那些事

    执行同样的存储过程,调用同样的参数 在VS 中调用存储过程和传参后,到数据加载需要20秒或更多, 在SQL直接调用则不到一秒,同一个存储过程为什么有这么大的区别呢? 原因:存储过程计划失效的原因 产生 ...

  9. oracle创建临时表空间、用户表空间、创建用户关联表空间、授权等

    1.创建临时表空间 CREATE TEMPORARY TABLESPACE test_temp TEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test ...

  10. CPU位数、操作系统位数、机器字长、C/C++基本数据类型长度

    1.CPU位数=CPU中寄存器的位数=CPU能够一次并行处理的数据宽度(位数)=数据总线宽度: //现在的计算机处理器一般都是64位,这是硬件的事. 2.CPU为了实现其功能设计了指令集,即是CPU的 ...