PAT A1020 Tree Traversals(25)
题目描述
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.
给出一个二叉树的后序遍历序列和中序遍历序列,求出这棵二叉树的层序遍历序列。
输出格式
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), 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.
输出格式
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.
输入样例
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例
4 1 6 3
《算法笔记》中AC答案
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 50;
struct node {
int data;
node* lchild;
node* rchild;
};
int pre[maxn], in[maxn], post[maxn]; //先序, 中序, 后序
int n; // 结点个数
// 当前二叉树的后序序列区间为[postL, postR], 中序序列区间为[inL, inR]
// create 函数返回构建出的二叉树的根结点地址
node* create(int postL, int postR, int inL, int inR) {
if(postL > postR) {
return NULL; // 后序序列长度小于等于0时,直接返回
}
node* root = new node; // 新建一个新的结点,用来存放当前二叉树的根结点
root->data = post[postR]; //新结点的数据域为根结点的值
int k;
for(k = inL; k <= inR; k++) {
if(in[k] == post[postR]) { // 在中序序列中找到in[k] == pre[L]的结点
break;
}
}
int numLeft = k - inL; // 左子树的结点个数
// 返回左子树的根结点地址,赋值给root的左指针
root->lchild = create(postL, postL + numLeft - 1, inL, k - 1);
// 返回右子树的根结点地址,赋值给root的右指针
root->rchild = create(postL + numLeft, postR - 1, k + 1, inR);
return root; // 返回根结点地址
}
int num = 0; // 已输出的结点个数
void BFS(node* root) {
queue<node*> q; //注意队列里是存地址
q.push(root); // 把根结点地址入队
while(!q.empty()) {
node* now = q.front(); // 取出队首元素
q.pop();
printf("%d", now->data);
num++;
if(num < n) printf(" ");
if(now->lchild != NULL) q.push(now->lchild); // 左子树非空
if(now->rchild != NULL) q.push(now->rchild); // 右子树非空
}
}
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &post[i]);
}
for(int i = 0; i < n; i++) {
scanf("%d", &in[i]);
}
node* root = create(0, n - 1, 0, n - 1); // 建树
BFS(root); // 层序遍历
return 0;
}
PAT A1020 Tree Traversals(25)的更多相关文章
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT 1060 爱丁顿数(25)(STL-multiset+思路)
1060 爱丁顿数(25 分) 英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个"爱丁顿数" E ,即满足有 E 天骑车超过 E 英里的最大整数 E.据说爱 ...
- PAT 1065 单身狗(25)(STL-map+思路+测试点分析)
1065 单身狗(25 分) "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ ...
- PAT 1070 结绳(25)(代码)
1070 结绳(25 分) 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每 ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...
- PAT 2-10. 海盗分赃(25)
题目链接:http://www.patest.cn/contests/ds/2-10 解题思路:参考:http://blog.csdn.net/linsheng9731/article/details ...
- [PAT] A1020 Tree Traversals
[题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ...
随机推荐
- topcoder13444
CountTables TopCoder - 13444 sol:题意和题解都丢在上面了,自己XJByy了一下 先保证行不同,然后对列容斥,dp[i]表示i列的答案 行不同时i列的答案显然是C(c^i ...
- 7.12T1序列
1.序列 [问题描述] Hzy 得到了一个字符串,这个字符串只有’A’,’G’,’C’,’T’这四种字符,她发现这个 序列中连续 k 个字符可以形成一种新的字符序列,她称这种序列为 Hzy 序列,她现 ...
- JAVA单元测试的用法和要点
2018年09月25日 10:11:18 琼歌 阅读数 5192 版权声明:禁止转载 https://blog.csdn.net/qq_36505948/article/details/827 ...
- No Desktop License Servers available to provide a license
远程桌面连接失败,提示:“no Remote Desktop License Servers available to provide a license” 原因:没有remote desktop l ...
- mysql:启动服务时遇到的问题
1.cmd命令: 在切换路径时,如果要切到另外一个磁盘,比如从C盘切到E盘,命令如下: cd /d 你要切换的路径 2.错误:“服务名无效” 问题原因:mysql服务没有安装.(参考:https:// ...
- nginx 反向代理实现负载均衡*理论
Nginx负载均衡集群介绍 负载均衡集群提供了一种廉价,有效,透明的方法,来扩展网络设备和服务器的负载,带宽和吞吐量,同时加强了网络数据处理能力,提高了网络的灵活性和可用性. 搭建负载均衡服务的需求: ...
- Qt编写数据可视化大屏界面电子看板3-新建布局
一.前言 能够新建布局,也是数据可视化大屏界面电子看板系统中的必备功能之一,新建布局这样的功能一般做到右键菜单中,单击新建布局菜单,弹出输入框要求输入新的布局的名称,为了更符合国情,直接支持中文名称, ...
- 九十五:CMS系统之cms后台模板渲染
定义一个宏,用于渲染static文件的时候,只需要传文件名就可以,上下两个“-”是解决渲染的时候源代码换行的情况 {% macro static(filename) -%} {{ url_for('s ...
- 数组setArray和addObjectsFromArray的区别
-setArray:用另一个数组中的所有对象来替换当前数组中的所有对象 -addObjectsFromArray:在原数组最后添加另一个数组的全部对象 NSArray *arr = @["] ...
- openstack——Rabbitmq集群部署
一.前期准备 1.条件:准备3台Linux系统虚拟机,保持系统版本一致,确保配置好yum源,及网络源 2.3台虚拟机做静态解析 [root@yun1 ~]# cat /etc/hosts 12 ...