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 ...
随机推荐
- RBAC权限控制
1.什么是RBAC权限模型rity2.RBAC权限模型表设计3.整合Mybatis数据库4.UserDetailsService5.动态查询数据库登陆6.动态权限角色拦截 什么是RBAC权限模型r 基 ...
- spark SQL学习(load和save操作)
load操作:主要用于加载数据,创建出DataFrame save操作:主要用于将DataFrame中的数据保存到文件中 代码示例(默认为parquet数据源类型) package wujiadong ...
- 鼠标指向表格时 显示更多信息 toolTipController1
//窗体添加 控件 ,然后将GridControl 相关属性栏对应上新加的控件 切记 本文是转帖,稍作修改 private void toolTipController1_GetActiveObjec ...
- HttpClient发送Json数据到指定接口
项目中遇到将Json数据发送到指定接口,于是结合网上利用HttpClient进行发送. /** * post发送json数据 * @param url * @param param * @return ...
- 从Activity中返回数据
从Activity中返回数据 一.简介 这里也就是使用intent方式返回数据. 二.具体步骤 在MainActivity通过一个button访问Activity01页面,然后将Activity01页 ...
- yii2: 上传图片,生成目录
1.单个文件上传 首先建立一个模型models/UploadForm.php,内容如下 namespace app\models; use yii\base\Model; use yii\web\Up ...
- 【LABVIEW到C#】4》String的操作之Search and Replace.vi
C#封装如下: public class SearchAndRepalce : Darrenstring { public bool replaced; private string stringou ...
- OpenCV几种边缘检测的简例
简单记录一下OpenCV的几种边缘检测函数的用法. 边缘检测算法 以Sobel边缘检测算法为例. Sobel卷积核模板为: 偏导公式为: Gx(i,j)=[f(i+1,j−1)+2f(i+1,j)+f ...
- hdu 5975 Aninteresting game
Aninteresting game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 【scala】占位符
Scala语言为了让函数字面量更加精简,还可以使用下划线作为占位符,用来表示一个或多个参数. 我们用来表示的参数必须满足只在函数字面量中出现一次. 我们用例子来看占位符的用法 scala> va ...