PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
题目解析
本题第一行给出二叉树的结点数量n,之后第二行给出二叉树的后序遍历,第三行给出二叉树的中序遍历,要求输出二叉树的层序遍历。(保证树中的每个结点数据都不同)
后序遍历中最后一个元素便是当前树的根结点,在中序遍历中找到根结点对应的值,其左侧便是左子树的中序遍历,在知道左子树中序遍历后可以根据结点数量在后续遍历中找到左子树的后续遍历,确定了左子树与根结点后剩下的便是右子树信息了,以此类推可以得到整棵二叉树。
建立二叉树后使用广搜,由于广搜二叉树维护队列时,每次出队一个结点并将其对应的左孩子与右孩子入队,这就是层序遍历。
AC代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int postorder[maxn]; //储存后续遍历
int inorder[maxn]; //储存中序遍历
struct node{ //二叉树结点信息
int data;
node *lchild;
node *rchild;
node(int tdata){ //构造函数
data = tdata;
lchild = NULL;
rchild = NULL;
}
};
node *create(int postL, int postR, int inL, int inR){
//由于要递归建树我们并不需要将每个树的左子树右子树的后序遍历与中序遍历储存下来
//只需要在原始postorder与inorder数组中标记出其位置即可
//postL与postR表示当前后续遍历的左右两端,inL与inR表示当前中序遍历的左右两端
if(postL > postR) //如果后续遍历左端位置大于右端位置,证明当前正在建立的树为空树
return NULL;
node *root = new node(postorder[postR]); //建立根结点,权值为postorder[postR]左右子树都为空
int i;
for(i = inL; inorder[i] != postorder[postR]; i++); //在中序遍历中找到根结点
int lenl = i - inL; //记录左子树长度
root->lchild = create(postL, postL + lenl - , inL, inL + lenl - );
//建立左子树
root->rchild = create(postL + lenl, postR - , inL + lenl + , inR);
//建立右子树
return root;
}
int n; //n记录结点数量
void levelorder(node *root){ //层序遍历
if(root == NULL)
return;
queue<node*> Q;
Q.push(root); //根结点入队
int cnt = ; //记录当前已经输出了几个数,用于之后判断是否输出空格
while(!Q.empty()){
cnt++;
node *top = Q.front();
Q.pop();
printf("%d", top->data);//输出结点权值
if(cnt != n)
printf(" ");
if(top->lchild != NULL) //左儿子入队
Q.push(top->lchild);
if(top->rchild != NULL)//右儿子入队
Q.push(top->rchild);
}
}
int main(){ scanf("%d", &n); //输入结点数量
for(int i = ; i < n; i++)
scanf("%d", &postorder[i]); //输入后序遍历
for(int i = ; i < n; i++)
scanf("%d", &inorder[i]); //输入中序遍历
node *root = create(, n - , , n - ); //建树
levelorder(root); //输出层序遍历
return ;
}
PTA (Advanced Level) 1020 Tree Traversals的更多相关文章
- PAT (Advanced Level) 1020. Tree Traversals (25)
递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...
- PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- 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)(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
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- 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 ...
随机推荐
- rabbitmq 简单应用
1. 启动 rabbitmq-server & 2. 队列重置(清空队列.用户等) rabbitmqctl stop_apprabbitmqctl resetrabbitmqctl stop ...
- centos 中 mongodb 启动失败的修复
mongodb是使用centos的yum命令安装的,整个的安装过程如下: 1. 运行 yum info mongo-10gen查看是否有mongodb源,如有跳至第3步. 2. 运行 vim /etc ...
- Xamarin.Android RelativeLayout
初次接触Xamarin.Android. 由于国内Xamarin的资料少见,我大多参考JAVA原生代码,慢慢摸索过来. 我把摸索出来的结果广而告之,希望后来人能少走一点弯路,也希望你也能做出一份贡献. ...
- AutoMapper之如何开始,适合入门和演示
原来想应该介绍下背景说明下好处什么的,仔细想都是废话 ,直接上代码吧. 首先有两个类,一个是和数据库对应的实体 Student,一个是和页面展示相关的页面模型 StudentModel. /// &l ...
- Android学习之Adapter(数据适配器)
1.定义 数据适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件. ...
- “全栈2019”Java第一百零四章:匿名内部类与外部成员互访详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Mybatis 动态Sql语句《常用》
MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...
- BZOJ 1801--中国象棋(DP)
1801: [Ahoi2009]chess 中国象棋 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1995 Solved: 1160[Submit] ...
- ubuntu15.04中安装mysql和mysql-workbench
本文主要讲解mysql在ubuntu中的安装和使用,以及mysql workbench的安装和使用.mysql是一个开源的数据库软件,甲骨文的一个产品,是一个性能较好的数据库软件.mysql work ...
- 类型转换:static_cast、reinterpret_cast等
一.隐式类型转换 系统自动进行,不需要程序开发人员介入. int m = 3 + 45.6;// 48 把小数部分截掉,也属于隐式类型转换的一部分 double b = 3 + 45.6; // 48 ...