【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意:
输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历。
trick:
当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结构体储存结点的序号(1~N),编号(代表它在完全二叉树上的位置)和值。PTA网站上可以用大小为31的数组存放结点,可能数据为一颗比较平衡的二叉树。
AAAAAccepted code:
/*
30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
*/
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
typedef struct node{
int data;
node *lchild,*rchild;
}tree;
tree *build(int l,int r,int L,int R){
if(l>r)
return NULL;
tree *temp=new tree();
temp->data=a[r];
int i;
for(i=L;i<=R;++i)
if(b[i]==a[r])
break;
temp->lchild=build(l,l+i-L-,L,i-);
temp->rchild=build(l+i-L,r-,i+,R);
return temp;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
for(int i=;i<=n;++i)
cin>>b[i];
tree *temp=build(,n,,n);
queue<tree *>q;
q.push(temp);
int flag=;
while(!q.empty()){
tree *now=q.front();
q.pop();
if(flag)
cout<<" ";
cout<<now->data;
flag=;
if(now->lchild)
q.push(now->lchild);
if(now->rchild)
q.push(now->rchild);
}
return ;
}
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int pos[],in[];
typedef struct lv{
int index,num;
};
lv level[];
int cnt=;
void find_(int iroot,int istart,int iend,int index){
if(istart>iend)
return;
level[++cnt].index=index;
level[cnt].num=pos[iroot];
int i=istart;
while(in[i]!=pos[iroot])
++i;
find_(iroot--iend+i,istart,i-,*index+);
find_(iroot-,i+,iend,*index+);
}
bool cmp(lv a,lv b){
if(a.index!=b.index)
return a.index<b.index;
}
int main(){
int n;
cin>>n;
for(int i=;i<n;++i)
cin>>pos[i];
for(int i=;i<n;++i)
cin>>in[i];
find_(n-,,n-,);
sort(level+,level++n,cmp);
for(int i=;i<cnt;++i)
cout<<level[i].num<<" ";
cout<<level[cnt].num;
return ;
}
【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)的更多相关文章
- 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)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
随机推荐
- 深度学习之反向传播算法(BP)代码实现
反向传播算法实战 本文仅仅是反向传播算法的实现,不涉及公式推导,如果对反向传播算法公式推导不熟悉,强烈建议查看另一篇文章神经网络之反向传播算法(BP)公式推导(超详细) 我们将实现一个 4 层的全连接 ...
- jvm01
hotspot:是jvm的核心组件(或者名称),jvm 需要对class文件进行编译成cpu能直接运行的代码.hotspot会对频繁使用的class代码进行缓存,不会再次编译,类似于缓存 client ...
- MyCat of Middleware
第一章 入门概述 1.1 是什么 Mycat 是数据库中间件. 1.数据库中间件 中间件:是一类连接软件组件和应用的计算机软件,以便于软件各部件之间的沟通. 例子:Tomcat,web中间件. 数据库 ...
- Python(一)list tuple dict set
这篇文章是为了复习之前学的python的数据结构: 原文链接:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a ...
- 《JavaScript高级程序设计》读书笔记(三)基本概念第二小节 Number类型
内容---语法 上一小节---数据类型 本小节 number类型---流程控制语句---理解函数 number类型--使用IEEE754格式来表示整数和浮点数值(双精度数值)--规定了数值字面量格式, ...
- How2j学习java-2、用命令行中编写第一个 JAVA 程序
真正在工作中开发 java 应用都会使用eclipse,myeclipse, IntelliJ等等 使用最原始的命令行方式来执行Hello World 1.准备项目目录 在e: 创建一个project ...
- Django - Form嵌套的Meta类 + 为什么type()能创建类
Form里面嵌套了一个Meta类 class PostForm(forms.ModelForm): class Meta: model = Post # field to be exposed fie ...
- Flask - g变量
传送门 http://flask.pocoo.org/docs/1.0/appcontext/#storing-data http://flask.pocoo.org/docs/1.0/appcont ...
- 创业学习--《预判行业机会》--B-2.预判模块---HHR计划--以太一堂
一,<开始学习> 1,行业机会的判断,是可以通过不断地训练提高自己的判准的概率的,要科学思考创业. 2,创业者在行业机会上的三个问题: a. 对市场变化,敏感性太弱,没有洞察行业的意识. ...
- linux 环境下安装 MySQL
参考: Linux安装MySQL5.7 注意: 安装后 出现 navicat MySQL连接Linux下MySQL的及2003错误 需要看下 虚拟机的防火墙是否关闭!!!