【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 ...
随机推荐
- Vim常用按键大全
Vim完全可以用键盘进行操作.本文将常用的按键归纳总结. 第一部分:一般模式可用的按钮,如光标移动.复制粘贴.查找替换等 移动光标的方法 h, j, k, l 光标向左,下,上,右移动 Ctrl + ...
- [Python] Tkinter的食用方法_02_LabelFrame RadioButton CheckButton
#开始 Python的话 我是看的小甲鱼的视频 所以代码也是小甲鱼的修改版 本博客这里只是学习记录 小甲鱼是个很棒的老师,虽然经常飙车.... #第一个代码 from tkinter import * ...
- JavaScript 对象的深复制
对象的深复制 源对象的属性更改,不会引起复制后的对象个属性的更改 源对象的任何属性与子属性与新对象的之间没有任何引用关系 Coding: /* 对象的深复制: 1 初始化目标对象 如果没有指定目标对象 ...
- Java数组和方法
1. 数组可以作为方法的参数 package cn.itcast.day05.demo04; /* 数组可以作为方法的参数. 当调用方法的时候,向方法的小括号进行传参,传递进去的其实是数组的地址值. ...
- 简单oracle查询语句转换为mongo查询语句
可以将简单的单表查询语句转换成Mongo过滤条件 列: 1. db.demo.aggregate([ {"$match": {"$and": [{"p ...
- 通过命令创建Django项目
本人是使用window10操作系统进行讲解Django框架,Linux系统和windows版本几乎一致,可以自行学习就可以解决. 首先在系统上创建了虚拟环境,如果不会创建,可以根据这篇文章学习:htt ...
- Python:面向对象基础
基本理论 什么是对象 万物皆对象 对象是具体的事物 拥有属性.行为 把许多零散的东西,封装成为一个整体 Python中一切东西都是对象,Python是一门特别彻底的面向对象编程语言(OOP) 其他编程 ...
- Composer包收录
doctrine/annotations #注解 nesbot/carbon #日期和时间处理 gregwar/captcha symfony/console nikic/fast-route #路由 ...
- 【洛谷P3500】TES-Intelligence Test
前言 先是这位神仙写了这道题 \(O(n\log n)\) 的做法.然后去他的博客上恰了一波. 然后发现这道题有 \(O(n)\) 的做法的.其实也不难. 题目 题目链接:https://www.lu ...
- Jmeter_请求原件之参数化txt
把数据存放在TXT上进行参数化,然后运行 用于注册,登录等不同的用例 1.登录接口地址: http://test.lemonban.com/futureloan/mvc/api/member/logi ...