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 ...
随机推荐
- 一种Web服务的go语言实现
0.引言 go语言已成为当今web后台开发的首选语言,关键在于其简洁性和高效并发特性.go中提供了丰富通用的http开发接口,但一般需要对其进一步封装才能更好的用于实际项目中.因此,本文基于开源库(g ...
- 「BZOJ 3242」「NOI 2013」快餐店「基环树」
题意 基环树上找到一个点(可以在边上)使得它到树上最远点的距离最小,输出最小距离 题解 如果是一棵树,答案就是树的直径\(/2\) 如果是基环树,那么很好证明删去环上的某一条边是不影响答案的.于是断环 ...
- Java与C++比较
本文仅从片面的角度比较Java与C++的一些特性,如有错误的地方,请指正. 语言特性上的一些差异: 1.Java没有无符号整数,C++/C#都有. 2.Java中不存在指针.Java的引用是功能弱化的 ...
- 【转】Linux将composer的bin目录放到PATH环境变量中
将composer的bin目录放到PATH环境变量中 使用composer global config bin-dir --absolute查看composer的bin目录 输出类似 Changed ...
- Nginx+ISS+Redis实现完美负载均衡
前篇文章讲到nginx是使网站采用分布式,对用户的请求采用分布式,分配到不同的服务器上,然后进行同一站点的访问,保证了访问的高效,使用率高,生命期长. 说到ISS,这里重点介绍tomcat,Tomca ...
- Ubuntu1804登录界面闪退
目前主力机操作系统已经由Ubuntu 16.04 lts升级到Ubuntu 18.04 lts.由于是跨版本升级过来,而且由unity(个人觉得挺好)替换成了gnome3,经常出点小问题.这次由于安装 ...
- 公有云Docker镜像P2P加速之路:安全篇
一.问题 在使用Docker运行容器化应用时,宿主机通常先要从Registry服务(如Docker Hub)下载相应的镜像(image).这种镜像机制在开发环境中使用还是很有效的,团队成员之间可以很方 ...
- 【三支火把】--- shell脚本中变量的类型及作用域
一直对shell脚本有一种特殊的感觉,因此花了一段时间学习,本人擅长C语言编程,深受C语言荼毒,在学习其他任何类似于编程语言的东东的时候,都会不自觉的与C进行对比,因此对于shell中的变量的作用域一 ...
- Qt 学习之路 2(7):MainWindow 简介
Qt 学习之路 2(7):MainWindow 简介 豆子 2012年8月29日 Qt 学习之路 2 29条评论 前面一篇大致介绍了 Qt 各个模块的相关内容,目的是对 Qt 框架有一个高屋建 ...
- [JavaScript] New-Agnostic Constructor Pattern
function User (name, password) { var self = this instanceof User ? this : new User(); if (name != nu ...