1020. Tree Traversals (25) ——树的遍历
//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印
PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难
先通过后续中序建一棵树,然后通过BFS遍历这棵树
提供测试样例
4
4 1 3 2
2 3 1 4
10
10 7 6 9 8 5 4 1 3 2
7 10 6 2 5 9 8 3 1 4
//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std; int back[];
int mid[];
int n; struct data{
int v;
int no;
data *left,*right;
}; data *head=new data;
data *rhead=head;
void build(){
int i,j;
head->v=back[n];
head->left=NULL;
head->right=NULL; int root=n,rj;
for(j=;j<=n;j++){
if(back[n]==mid[j]){
head->no=j;break;
}
}
for(i=n-;i>=;i--){
for(j=;j<=n;j++){
if(back[i]==mid[j]){
rj=j;break;
}
}
head=rhead;
while(){
if((rj < head->no)&&head->left!=NULL){
head=head->left;continue;
}
if((rj > head->no)&&head->right!=NULL){
head=head->right;continue;
}break;
}
if(rj < head->no){
head->left=new data;
head=head->left;
head->v=back[i];
head->no=rj;
head->left=NULL;
head->right=NULL;
}else{
head->right=new data;
head=head->right;
head->v=back[i];
head->no=rj;
head->left=NULL;
head->right=NULL;
}
} } void bfs(){
data *first,*second;
first=rhead;
queue<data *>qq;
qq.push(first); int ok=;
while(!qq.empty()){ if(ok==){
ok=;
printf("%d",qq.front()->v);
}else{
printf(" %d",qq.front()->v);
}
second=qq.front();
qq.pop();
if(second->left!=NULL){
qq.push(second->left);
}
if(second->right!=NULL){
qq.push(second->right);
}
}
printf("\n");
} int main()
{
while(scanf("%d",&n)!=EOF){
int i;
head=new data;
rhead=head;
for(i=;i<=n;i++){
scanf("%d",&back[i]);
}
for(i=;i<=n;i++){
scanf("%d",&mid[i]);
}
build();
bfs();
} return ;
}
1020. Tree Traversals (25) ——树的遍历的更多相关文章
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 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 (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甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- 1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020. Tree Traversals (25) -BFS
题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
随机推荐
- 织梦dedecms5.7二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法
dedecms升级到5.7SP1后,开启二级域名,你会发现,在二级域名下的文章,上传的图片地址都是:二级域名+图片地址,这是相对路径.但是你的图片是默认上传在uploads里面的.应该使用绝对路径.这 ...
- Hibernate -- 对象关系映射基础
- 简单Trace类实现
<C++沉思录>27章内容修改后所得: /************************************************************************/ ...
- 测绘类SCI
GeoInformatica(国际地理信息系统计算机科学进展杂志)美国International Journal of Geographical Information Science(国际地理信息科 ...
- 在数据库中添加数据以后,使用Mybatis进行查询结果为空
在数据库中添加数据以后,使用Mybatis进行查询结果为空,这是因为数据库中添加数据忘记commit的缘故.
- WAF 强化学习
参考:https://github.com/duoergun0729/3book/tree/master/code/gym-waf 代码: wafEnv.py #-*- coding:utf-8 –* ...
- iOS自动化探索(一)WebDriverAgent安装
WebDriverAgent FaceBook推出的一款iOS移动测试框架, 支持真机和模拟器, 同时支持USB, 官方是这样介绍的: https://github.com/facebook/WebD ...
- 双系统在Linux下查看win的硬盘(Ubuntu 16.04 挂载Windows的 硬盘)
一般情况下,Linux的桌面系统能够直接查看到计算机各个硬盘的文件情况 但是,当我们想通过命令行查看Windows下的硬盘的时候,会发现在 /media/ (一般Windows下的盘会挂载到这里)文件 ...
- 条款24:如果所有的参数都需要类型转换,那么请为此采用non-member函数
首先还是下面这个class; class Rational{ public: Rational(, ); int numurator() const; int denominator() const; ...
- DataV纪录
DataV 是阿里云出品的拖拽式可视化工具,专精于业务数据与地理信息融合的大数据可视化.