pat1020. Tree Traversals (25)
1020. Tree Traversals (25)
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<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
int post[],in[];
struct node{
int v;
node *l,*r;
node(){
l=r=NULL;
}
};
void BuildTree(int *post,int *in,int n,node *&h){
if(!n){
return;
}
h=new node();
int mid=post[n-];
h->v=mid;
int i;
for(i=;i<n;i++){
if(in[i]==mid){
break;
}
}
BuildTree(post,in,i,h->l);
BuildTree(post+i,in+i+,n--i,h->r);
}
int main(){
int n,i;
node *h;
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&post[i]);
}
for(i=;i<n;i++){
scanf("%d",&in[i]);
}
BuildTree(post,in,n,h);
node cur;
queue<node> q;
q.push(*h);
printf("%d",h->v);
while(!q.empty()){
cur=q.front();
q.pop();
if(cur.l!=NULL){
q.push(*(cur.l));
printf(" %d",cur.l->v);
}
if(cur.r!=NULL){
q.push(*(cur.r));
printf(" %d",cur.r->v);
}
}
return ;
}
pat1020. Tree Traversals (25)的更多相关文章
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...
- 1020. Tree Traversals (25) -BFS
题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- 1020 Tree Traversals (25)(25 point(s))
problem Suppose that all the keys in a binary tree are distinct positive integers. Given the postord ...
随机推荐
- Server.MapPath方法的应用方法
老是忘记Server.MapPath的使用方法了,下面记录一下,以备后用:总注:Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径1.Server.MapPath(&q ...
- 多用户ATM机(面向对象编程)
let readline = require('readline-sync'); // 清屏函数 let clear = () => process.stdout.write(process.p ...
- VB.NET提取TXT文档指定内容
今天有浏览论坛时,又看见一篇是读取TXT文本文件的论题.Insus.NET也想以自己的想法来实现,并分享于此. 文本文件是比较复杂,获取数据也是一些文本行中取其中一部分.为了能够取到较精准的数据,In ...
- Kotlin 数据类型(字符类型)
Kotlin有两种常见的字符类型. 第一种是String,第二种char. String String 的类型的赋值必须是用双引号的. 格式: var name="Arm830" ...
- leftjoin及多个leftjoin执行顺序
给个通俗的解释吧.例表aaid adate1 a12 a23 a3表bbid bdate1 b12 b24 b4两个表a,b相连接,要取出id相同的字段select * from a inner jo ...
- bzoj 3131 [Sdoi2013]淘金(数位dp)
题目描述 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块. 一阵风吹过,金子的位置发生了 ...
- cenos php执行pdf2swf 配置环境
1.第一步:安装xpdf语言包 1.mkdir –p /usr/share/xpdf 2.cd /usr/share/xpdf/ 3.下载中文支持及字体库wget ftp://ftp.foolabs. ...
- OkHttp 3.x 源码解析之Interceptor 拦截器
拦截器 Java里的拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提 ...
- ipad中icon与launchimage的size
图标(AppIcon)与启动图(LaunchImage)是开发iOS应用程序必不可少的内容,但是在网络上对于这部分的内容讲解的并不详细,所以花些时间写了这篇文章,希望有需要的朋友可以随时查看 想知道A ...
- BZOJ 2725 [Violet 6]故乡的梦 线段树+最短路树
\(\color{#0066ff}{ 题目描述 }\) \(\color{#0066ff}{输入格式}\) \(\color{#0066ff}{输出格式}\) \(\color{#0066ff}{输入 ...