题意:

输入一个正整数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 分)(树知二求一)的更多相关文章

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

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

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

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

  3. PAT Advanced 1020 Tree Traversals (25 分)

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

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

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

  5. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  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. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

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

  8. 1020 Tree Traversals (25分)思路分析 + 满分代码

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

  9. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

随机推荐

  1. Java中List集合的逆序排列

    Collections.reverse(list);  //实现List集合逆序排列

  2. SpringBoot 开发的那些小趣事儿

    经过这次在公司实习中获取到的经历,我发现确实有时候书本上的知识发挥的作用微乎其微,好像是被问题打了太极拳一样,你明明想去攻克这个地方,他却给你报了其他地方的错误. 平常的一些小项目根本就不能匹配到企业 ...

  3. java8下 枚举 通用方法

    在项目中经常用到枚举作为数据字典值和描述的相互转化. 用法如下: public enum CommunicationParamsCom { COM_1(1, "COM1"), CO ...

  4. 01-Spring的概述

    Spring概述 ①Spring是一个开源框架 ②Spring为简化企业级开发而生,使用Spring开发可以将Bean对象,Dao组件对象,Service组件对象等交给Spring容器来管理,这样使得 ...

  5. 如何修改mysql 默认引擎为InnoDB?

    1.查看 MySQL支持的引擎有哪些? show engines: 结果: 由上图可以看出,只有 InnoDB 是支持事务的 2.查看默认引擎 show variables like “default ...

  6. 看 C++ Primer 的58页, 讲拷贝时不能忽略 底层const这里的说的原因有点牵强, 这里给出自己的理解

    extern const  int ci =42; const int * p2  = &ci; extern const  int *const p3= p2; int *p= p3;   ...

  7. Scrapy模拟登陆

    1. 为什么需要模拟登陆? #获取cookie,能够爬取登陆后的页面 2. 回顾: requests是如何模拟登陆的? #1.直接携带cookies请求页面 #2.找接口发送post请求存储cooki ...

  8. Java常量/变量

    1. 常量 /* 常量:在程序运行期间,固定不变的量. 常量的分类: 1. 字符串常量:凡是用双引号引起来的部分,叫做字符串常量.例如:"abc"."Hello" ...

  9. VMware15下载、安装、激活

    1.VMware15下载 链接:https://pan.baidu.com/s/1bI8LReRY-5k81O3rrNgg-A  提取码:6c03 2.VMware15安装 3.VMware15激活

  10. jvm 内存,线程,gc分析

    1.查看 gc的次数,和各个垃圾回收区域的内存比例  jstat : jstat -gcutil pid interval(ms) 例子:jstat -gcutil 332 1000 参数说明如下: ...