当前位置:网站首页>Output first order traversal according to second order and middle order traversal (25 points)
Output first order traversal according to second order and middle order traversal (25 points)
2022-04-23 08:41:00 【Huaihua second affectionate】
This problem requires that according to the post order traversal and middle order traversal results of a given binary tree , Output the pre order traversal result of the tree .
Input format :
The first line gives a positive integer N(≤30), Is the number of nodes in the tree . The next two lines , Each line gives N It's an integer , Corresponding to post order traversal and middle order traversal results respectively , Numbers are separated by spaces . The title ensures that the input is correct and corresponds to a binary tree .
Output format :
Output in one line Preorder: And the pre order traversal result of the tree . Between the numbers 1 A space , 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 :
Preorder: 4 1 3 2 6 5 7
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <queue>
#include<map>
using namespace std;
const int N = 40;
int n,count1;
int postorder[N], inorder[N]; // The former sequence traversal , In the sequence traversal
unordered_map<int, int> l, r, pos; // Simulating binary tree with hash table
int build(int il, int ir, int pl, int pr)
{
int root = postorder[pr];
int k = pos[root]; // Get the subscript of the root node in the middle order traversal
//k Greater than il Indicates that there are nodes on the left of the root node , That is, the current root node has a left subtree , The same below
// Here are two difficult lines , See the figure for example
if (il < k) l[root] = build(il, k - 1, pl, pl + k - 1 - il);
if (ir > k) r[root] = build(k + 1, ir, pl + k - il, pr - 1);
return root;
}
void dfs(int root) //BFS Used to traverse the output
{
if(count1==0)
cout<<root;
else cout<<" "<<root;
count1++;
if(l.count(root))dfs(l[root]);
if(r.count(root))dfs(r[root]);
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> postorder[i];
for (int i = 0; i < n; i ++ )
{
cin >> inorder[i];
pos[inorder[i]] = i; // Record the position of each point in sequence ( prune )
}
int root = build(0, n - 1, 0, n - 1); // The parameters are middle order traversal interval and post order traversal interval
printf("Preorder: ");
dfs(root);
return 0;
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222541.html
边栏推荐
- SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
- Go语言自学系列 | golang嵌套结构体
- 引用传递1
- Detailed description of self feeling of auricular point weight loss 0422
- 汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
- 'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
- Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
- Stm32f103zet6 [development of standard library functions] - Introduction to library functions
- 关于堆的判断 (25 分) 两种插入方式
- Idea import commons-logging-1.2 Jar package
猜你喜欢

四张图弄懂matplotlib的一些基本用法

增强现实技术是什么?能用在哪些地方?

idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)

【58】最后一个单词的长度【LeetCode】

ESP32程序下载失败,提示超时

MySQL查询两张表属性值非重复的数据

On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched

Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download

Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动

虚拟线上展会-线上vr展馆实现24h沉浸式看展
随机推荐
STM32使用HAL库,整体结构和函数原理介绍
使用flask和h5搭建网站/应用的简要步骤
IDEA导入commons-logging-1.2.jar包
LeetCode396.旋转数组
Enctype attribute in form
正点原子携手OneOS直播 OneOS系统教程全面上线
玩转二叉树 (25 分)
2022-04-22 OpenEBS云原生存储
Redis master-slave server problem
2021李宏毅机器学习之Adaptive Learning Rate
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
Introduction to protobuf
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
虚拟线上展会-线上vr展馆实现24h沉浸式看展
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
Go语言自学系列 | golang结构体作为函数参数
Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)
深度学习框架中的自动微分及高阶导数
经典题目刷一刷