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 ...
随机推荐
- java 类的加载、连接和初始化
JVM和类 调用Java命令运行Java程序时,该命令将会启动一条Java虚拟机进程,不管该Java程序启动了多少条线程,创建了多少个变量,它们都处于该Java虚拟机进程里,共享该JVM进程的内存区. ...
- JSP导入包
1. <%@page import="java.util.Date, mianBao.*, z_utils.*"%> 2. 3.
- java-四则运算-四
题目要求:查找数组连成环形的和最大的连续子数组 实验代码: package zuoYe; import java.util.Scanner; public class MaxSubArray { pu ...
- 带你彻底明白 Android Studio 打包混淆
前言 在使用Android Studio混淆打包时,该IDE自身集成了Java语言的ProGuard作为压缩,优化和混淆工具,配合Gradle构建工具使用很简单.只需要在工程应用目录的gradle文件 ...
- fiddler之使用教程(一)
一. 什么是fiddler&它可以做什么 fiddler是位于客户端和服务器端的HTTP代理,也是目前最常用的http抓包工具之一.它能够记录客户端和服务器之间的所有HTTP请求,可以针对特定 ...
- SpringBoot下的Dubbo和Zookeeper整合
最近一直在学各种分布式的内容,学到了dubbo分布式服务框架就想写个小demo使用一下,但是由于我要整合在SpringBoot框架中,SpringBoot框架毕竟提倡的是java注解配置,XML文件不 ...
- Windows API的消息处理机制
上个学期找实习失利,让我觉得自己基础打得不够牢固,所以在华为实习的这三个月里,每天下班都在复习理论课的知识,顺便刷了一个月的 LeetCode.本来以为找工作是势在必得了,结果这个学期秋季校招的坑爹经 ...
- CC国内厂商现状
(1)阿里云的产品 CC攻击 攻击者攻击服务器的认证页面.登录页面.游戏论坛等.还是用饭馆的例子,CC攻击相当于,坏人霸占收银台结账.霸占服务员点菜,导致正常的客人无法享受到服务. 游戏盾如何防御CC ...
- dga 分析
02n-0iy6gn3ozzwmyu.7i43n9qil1g1z2-.com0e527eaf_5ec5_4623_9fe9_e459583acd72.com0fmgm1cuu7h1279dghgka0 ...
- 【辅助工具】Python实现微信跳一跳
最近迷上了微信跳一跳小游戏,正好也看到知乎上有大神分享了技术贴,我也参考了好多资料,原理就是通过abd命令截取图片,python计算两个点距离,然后转化按压时间,让电脑来完成游戏.我花了很长时间才把程 ...