【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 ... 
随机推荐
- SOA架构设计
			架构是—套构建系统的准则,通过这套准则,把—个复杂的系统划 分为一套更简单的子系统的集合,这些子系统之间保持相互独立,并与 整个系统保持一致,而且每—个子系统还可以继续细分下去,从而构成 —个企业级架 ... 
- 日期格式在ios中的兼容性
			在IOS中支持 2017/3/2 这种格式的日期 不支持2017-3-2日期 /** * 返回兼容ios.android的日期时间格式 * @param dateTime String * @retu ... 
- nginx 解决 connect() failed (111: Connection refused) while connecting to upstream,
			嗯哼,刚装了个ubuntu的lnmp,我的天啊,踩的坑比我脂肪还多了 比如刚装完的时候访问显示502, 也不知道什么问题,就去看了一下nginx日志 /var/log/nginx/error.log ... 
- Airflow 操作知识总结(完善中)
			airflow默认以utc时区运行,如果需要计算正确的时间,需要把时间进行时区转换,核心代码如下 #将本地时间转换为utc时间,再设置为start_date tz = pytz.timezone('A ... 
- 交换机出现err-disable的原因及解决方法
			转:https://www.2cto.com/net/201303/198724.html 交换机出现err-disable的原因及解决方法 LOG示例: 21w6d: %ETHCNTR-3-LOOP ... 
- Python实现mysql数据库增删改查
			利用python操作mysql数据库用法简单,环境配置容易,本文将实现对库增.删.改.查的简易封装! 1. 环境配置 安装第三方包 ,导入模块 mysql.connector pip inst ... 
- 制作手风琴效果时发现新大陆,好吧,其实是一个bug
			手风琴效果代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ... 
- python导入openpyxl报错问题,终于解决啦
			问题:折腾了一上午,安装.卸载openpyxl多次,cmd中明明显示安装成功,可python文件import时就是报错 1.安装openpyxl后,python文件导入一直报错,经过一上午的努力,终于 ... 
- SpringBoot 配置 Redis 多缓存名(不同缓存名缓存失效时间不同)
			import com.google.common.collect.ImmutableMap; import org.springframework.cache.CacheManager; import ... 
- redis场景分析的很到位
			链接:http://www.zhihu.com/question/19829601/answer/88069207来源:知乎 1. MySql+Memcached架构的问题 实际MySQL是适合进行海 ... 
