【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 ...
随机推荐
- web开发中的一些不常见的概念
1.惊群 [活跃]星际争霸小王子 就是当你在车站时,一堆拉客的人一拥而上,想你坐他的车,于是就惊群了.但你只能坐一个车,所以没拉到你的就悻悻而归,于是return false[活跃]星际争霸小王 2 ...
- TensorFlow 模型的保存与载入
参考学习博客: # https://www.cnblogs.com/felixwang2/p/9190692.html 一.模型保存 # https://www.cnblogs.com/felixwa ...
- CentOS安装MySQL的步骤
1.下载 Mysql yum包 http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 下载到本地再上传到服务器,或者使用wget 直接下载 w ...
- 不要在mutation回调函数之外,修改vuex仓库里属性的状态
[vuex] do not mutate vuex store state outside mutation handlers. import * as types from './mutation- ...
- 无刷新的批量图片上传插件.NET版
啥都不说,先上效果图: 这是一个网上的第三方组件,原版是php的,我用.NET重写了图片上传的处理,下面贴上代码 using System; using System.Collections.Gene ...
- Spring Boot 学习前你应该知道的 Maven 知识
Maven 是什么? 回答这个问题,我们先来了解下没有Maven,我们是怎么使用开发者工具IDE去开发Java程序的.我之前开发Java程序不多,但是我还是记得,我是从网上下载或从合作方拷贝 jar ...
- Spring Boot RestApi 测试教程 Mock 的使用
测试 Spring Boot Web 的时候,我们需要用到 MockMvc,即系统伪造一个 mvc 环境.本章主要编写一个基于 RESTful API 正删改查操作的测试用例.本章最终测试用例运行结果 ...
- Nexus-vPC和STP BPDU
1.为了交互vPC拓扑,STP机制被修改适应到vPC peer环境.2.对于vPC ports,只有主角色运行STP,换句话说,vPC下的STP由主角色设备控制.3.只有主角色设备在DP(指定端口)上 ...
- Codeforces Round #620 (Div. 2) B. Longest Palindrome
Returning back to problem solving, Gildong is now studying about palindromes. He learned that a pali ...
- Servlet里面request处理外部POST请求的输入流的工具类
package etcom.servlet; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...