1020. Tree Traversals (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

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

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

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

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

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

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

  4. PAT Advanced 1020 Tree Traversals (25 分)

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

  5. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  6. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. 1020. Tree Traversals (25)

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...

  8. 1020. Tree Traversals (25) -BFS

    题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  9. 1020 Tree Traversals (25)(25 point(s))

    problem Suppose that all the keys in a binary tree are distinct positive integers. Given the postord ...

随机推荐

  1. 关于eclipse导入maven项目

    1:删除其他的配置文件,只需要源码 和 pom文件 2:导入项目,再修改几个地方: 2.1: 所选项目右键- properties - Project Facet,勾上 Dynamic Web Mod ...

  2. jmeter测试报告汉化及脚本编写

    在做接口自动化时,生成的测试报告页面是英文的,如现在我们优化成汉文.操作如下: 1,下载汉化包 下载路径:https://i.cnblogs.com/Files.aspx?order=1 2,解压汉化 ...

  3. ScriptableObject

    什么是ScriptableObject? 点击查看Unity官网的描述 直译过来就是“脚本化对象”,换言之这类作为存储结构化的数据来使用,并写入Unity的资源.asset文件去存储一组数据,取用的时 ...

  4. 【转】如何恶搞朋友的电脑?超简单的vbs代码

    源地址:https://jingyan.baidu.com/article/d3b74d64aa1e6a1f77e609e6.html 表白源地址:https://jingyan.baidu.com/ ...

  5. Atcoder CF 2017 TR I

    Atcoder CF 2017 TR I 给定一个有n个点,m条边的图,求为每条边定向,使得从1出发和2出发的两个人可以见面的方案数. 先把问题转换成求all-不能见面的方案数.那么可以把图划分成这样 ...

  6. cenos 上的php 支持GD库问题

    ---恢复内容开始--- thinkphp 开发的项目verify类无法引用,原因是没有开启gd库 环境:CentOS 6.4,php-5.3.3需求:php支持GD库解决方案:GD是Linux下的一 ...

  7. 《图解HTTP》阅读笔记--第七章---确保WEB安全的HTTPS

    第七章.确保WEB安全的HTTPSHTTP的缺点:通信使用明文(不加密),内容可能会被窃听 解决---加密处理: //将通信加密 :通过SSL(安全套接层)---HTTPS(超文本传输安全协议)--- ...

  8. P2155 [SDOI2008]沙拉公主的困惑

    \(\color{#0066ff}{ 题目描述 }\) 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大 ...

  9. Nginx01---简单使用

    基于腾讯云--ubuntu系统 1.安装nginx sudo apt-get install nginx 2.启动,停止nginx nginx -c /usr/local/nginx/conf/ngi ...

  10. fatal error C1859: “Release\IWBServer.pch”意外的预编译头错误,只需重新运行编译器就可能修复此问题

    解决方案 1.    创建预编译头(/Yc)   -- >     stdafx.cpp    使用预编译头(/Yu) 2.    complie 3.    使用预编译头(/Yu)    -- ...