当前位置:网站首页>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
边栏推荐
- Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
- cadence的工艺角仿真、蒙特卡洛仿真、PSRR
- Stm32f103zet6 [development of standard library functions] - Introduction to library functions
- LLVM之父Chris Lattner:编译器的黄金时代
- 玩转二叉树 (25 分)
- RCC introduction of Hal Library
- 在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
- php基于哈希算法出现的强弱比较漏洞
- Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
- Test your machine learning pipeline
猜你喜欢

RCC introduction of Hal Library
![Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]](/img/67/1f9df4236b0aac3480836d45ab8561.png)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

Chris LATTNER, father of llvm: the golden age of compilers

汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告

GUI编程简介 swing

Excle plus watermark

洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別

在sqli-liabs学习SQL注入之旅(第十一关~第二十关)

请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨

idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
随机推荐
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
Study notes of deep learning (8)
信息收集相关知识点及题解
队列(c语言/链表)
Queue (C language / linked list)
lgb,xgb,cat k折交叉验证
Go语言自学系列 | golang结构体的初始化
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
PgSQL wants to implement all kinds of column sub query operations of MySQL
耳穴减肥自身感受细节描述0422
idea打包 jar文件
ESP32程序下载失败,提示超时
根据字节码获取类的绝对路径
匿名類型(C# 指南 基礎知識)
How to generate assembly file
Go语言自学系列 | golang嵌套结构体
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
记录:js删除数组中某一项或几项的几种方法
Use of applicationreadyevent
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board