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 ...
随机推荐
- android中SharedPreferences 读取不到数据的问题
在两个不同的 Activity中,A中SharedPreferences保存了数据,在A中可以读取到,但是在 B中却读取不到了,一直是空值,好是不爽,由于是按照书本上的例子写的, 怎么也找不到原因,后 ...
- JetBrains Rider 在 Mac 环境下将 cs 文件生成 exe
因为自己的开发环境是 Mac + Rider 组合,想测试网络编程相关内容.想在Windows 虚拟机上运行一套代码来与Mac 机进行测试,但又不想在虚拟机上安装一套开发环境.最终找到的解决方案是通过 ...
- Python如何打包EXE可执行文件
一.安装Pyinstaller 使用Python打包EXE可执行文件时,需要使用Pyinstaller,使用命令pip install PyInstaller进行安装,如下图: 安装成功后的效果如下图 ...
- winform ComBox绑定数据
初始化数据: List<KeyValuePair<string, string>> list: ComBox1.ValueMember = "Key";Co ...
- Python3中集合的混合使用
比较简单没什么好说的: list_1 = [1,2,3,4,6,3,2,5,7,8,2,1] list_1 = set(list_1) list_1.add(999) list_2 = set([2, ...
- 基于pythpn的深度学习 - 记录
[基于pythpn的深度学习] 环境: windows/linux-ubuntu Tensorflow (基于anaconda) *安装 (python3.5以上不支持) ...
- Alyona and towers CodeForces - 739C (线段树)
大意: 给定序列, 要求实现区间加, 询问整个序列最长的先增后减的区间. 线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆int. #in ...
- Python中dataframe\ array\ list相互转化
import pandas as pd import numpy as np #创建列表 a1=[1,2,3] #arange函数:指定初始值.终值.步长来创建数组 a2=np.arange(0,1, ...
- day02.1-Linux虚拟操作系统的安装
在windons环境中配置Linux虚拟操作系统,需要事先在Windons系统上下载并安装虚拟运行软件“VMware Workstations”,以下虚拟物理机的建立和虚拟Linux系统的配置都是在该 ...
- 小程序渲染问题:ios显示安卓不显示
问题描述: 测试库转到正式库后添加数据,小程序数据渲染不出来,但是测试库没问题,ios数据能显示,没问题,但是安卓显示没数据. 排除是服务器https证书问题,如果是证书问题,小程序会直接调不了接口. ...