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. winform ComBox绑定数据

    初始化数据: List<KeyValuePair<string, string>> list: ComBox1.ValueMember = "Key";Co ...

  2. 动态合并Repeater控件数据列

    前天Insus.NET实现<动态合并GridView数据行DataRow的列>.今天再玩玩Repeater控件,功能也是动态合并某列栏位.Repeater控件跟GridView控件一样集成 ...

  3. 网易云易盾中标浙报反作弊服务 助力浙江新闻App健康发展

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 近日,国内领先的智能业务安全平台网易云易盾和浙报传媒旗下"浙江新闻"达成合作,易盾将为浙江新闻客户端提供大数据反作弊服务,助力浙 ...

  4. FPM包定制完成 (等待实现 里程碑 1 和 2) 2018年4月13日 2:18:32

    前期环境准备: 关闭SELINUX  :   setenforce 0 关闭SELINUX  :   sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' / ...

  5. 通过pip3安装virtualenvwrapper

    pip3 install virtualenvwrapper 配置virtualenvwrapper创建虚拟环境的目录和指定python3版本 环境编辑当前用户配置变量 mkdir ~/.virtua ...

  6. ASP.NET-GridView之表头设计

    我们 见过许多网页呈现表格的 时候,表头的形式多种多样.下面来看看,怎么制定多样的表头吧. 效果显示: 需要在后台写一个方法,网页一加载在前端写个事件调用这个方法. DEMO 前端 <span ...

  7. JavaScript之DOM HTML

    前言 JavaScript这门语言在一定程度上让我们html之间耦合度降低了,为什么这样说呢?JavaScript语言一样可以可以随意写入html页面一些东西,比如:JavaScript的DOM可以改 ...

  8. [Swift实际操作]九、完整实例-(7)登录页面:创建自定义视图及相关组件Swift实际操作

    本文将开始创建登录页面,首先创建该页面所需的一些自定义组件:做为登录按钮的自定义视图对象.在[RegLogin]组的名称上点击鼠标右键,打开右键菜单.[New File]->[Cocoa Tou ...

  9. truts2标签-forEach标签

    <c:forEach begin="1" end="${obj.portnum}" step="1" varStatus=" ...

  10. 【NOIP 2009】最优贸易

    描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通 ...