当前位置:网站首页>7-8 浪漫侧影(25分)建树+新解题思路
7-8 浪漫侧影(25分)建树+新解题思路
2022-07-28 16:33:00 【jsBeSelf】
浪漫侧影 (25 分)

题目描述
“侧影”就是从左侧或者右侧去观察物体所看到的内容。例如上图中男生的侧影是从他右侧看过去的样子,叫“右视图”;女生的侧影是从她左侧看过去的样子,叫“左视图”。
我们将二叉树的“侧影”定义为从一侧能看到的所有结点从上到下形成的序列。例如下图这棵二叉树,其右视图就是 { 1, 2, 3, 4, 5 },左视图就是 { 1, 6, 7, 8, 5 }。
于是让我们首先通过一棵二叉树的中序遍历序列和后序遍历序列构建出一棵树,然后你要输出这棵树的左视图和右视图。
输入格式:
输入第一行给出一个正整数 N (≤20),为树中的结点个数。随后在两行中先后给出树的中序遍历和后序遍历序列。树中所有键值都不相同,其数值大小无关紧要,都不超过 int 的范围。
输出格式:
第一行输出右视图,第二行输出左视图,格式如样例所示。
输入样例:
8
6 8 7 4 5 1 3 2
8 5 4 7 6 3 2 1
输出样例:
R: 1 2 3 4 5
L: 1 6 7 8 5
分析
1)建树直接根据模板写,然后得到tree数组。
2)输出右侧面就是从index=1开始,如果index×2+1存在结点就输出,不存在则查看index×2,若存在就输出,都不存在就从index×2开始,往前寻找到第一个结点即为最右的结点。
3)输出左侧面与右侧面类似,如果index×2存在结点就输出,不存在则查看index×2+1,若存在就输出,都不存在就从index×2开始,往后寻找到第一个结点即为最左的结点。
代码
#include <iostream>
#include <algorithm>
#include <cmath>
#define MAX 100000
using namespace std;
int n, midord[30], postord[30], tree[MAX], level = 0;
void createTree (int *postord, int *midord, int n, int index) {
if (!n) return ;
int k = 0;
while (postord[n - 1] != midord[k]) k ++;
tree[index] = midord[k];
level = max(level, index);
createTree(postord, midord, k, index * 2);
createTree(postord + k, midord + k + 1, n - k - 1, index * 2 + 1);
}
int main () {
cin >> n;
for (int i = 0; i < n; i ++) cin >> midord[i];
for (int i = 0; i < n; i ++) cin >> postord[i];
createTree(postord, midord, n, 1);
for (int i = 1; i <= 22; i ++)
if (level < pow(2, i)) {
level = i;
break;
}
cout << "R: ";
int cnt = 0, idx = 1;
while (cnt < level) {
if (tree[idx]) cout << tree[idx];
if (tree[idx * 2 + 1]) idx = idx * 2 + 1;
else if (tree[idx * 2]) idx = idx * 2;
else {
idx = idx * 2;
while (!tree[idx]) idx --;
}
cnt ++;
if (cnt != level) cout << ' ';
else cout << endl;
}
cout << "L: ";
cnt = 0, idx = 1;
while (cnt < level) {
if (tree[idx]) cout << tree[idx];
if (tree[idx * 2]) idx = idx * 2;
else if (tree[idx * 2 + 1]) idx = idx * 2 + 1;
else {
idx = idx * 2;
while (!tree[idx]) idx ++;
}
cnt ++;
if (cnt != level) cout << ' ';
else cout << endl;
}
return 0;
}
边栏推荐
- 软件测试和软件开发应该怎么选择?
- 解决Package is not available (for R ve【PACKAGE ‘XXX’ IS NOT AVAILABLE (FOR R VERSION X.Y.Z)” WARNING?】
- MySQL implements sorting according to custom (specified order)
- ggplot2地图
- 你能读懂这个故事吗?
- Database performance analysis and optimization (internal training materials of Aite future team)
- 【C语言笔记分享】字符函数和字符串函数(建议收藏)
- JVM性能调优
- 转行学习软件测试有前途吗?
- 软件测试培训两个月骗局?培训机构到底如何选择?
猜你喜欢

Public medical database

es6 Promise

【 R语言—基础绘图】

JDWP未授权快速利用

JVM performance tuning

地图R语言

Deploy lamp platform -- compilation and installation of Linux, Apache, MySQL and PHP

软件测试工程师如何突破职业发展瓶颈

Backup and restore of SNAT and DNAT firewall rules

Esp-mqtt-at instruction connects Alibaba cloud Internet of things platform
随机推荐
Visual object class introduces Pascal VOC dataset
软件测试工程师如何突破职业发展瓶颈
@RequestMapping详解
USB virtual serial port (CDC) limit speed test
Generation and use of dynamic link library (error summary)
软件测试的培训机构靠谱吗
Public medical database
AMQP protocol details
Arya professional web automated test platform
Awk of shell script
Adding new objects to the object array in JS results in the modification of existing objects in the array
ggplot2地图
如何看软件测试未来的发展?
Convert the image file of input type='file'to Base64
Hgu95av2. Online installation failed
一篇带你走近Kubernetes概貌与原理
谈谈“发布后问题”的度量
多线程(线程池ThreadPoolExecutor)
JVM performance tuning
Ggplot2 map