当前位置:网站首页>L2-006 tree traversal (25 points)
L2-006 tree traversal (25 points)
2022-07-06 11:26:00 【%xiao Q】
subject
Given a binary tree's post order traversal and middle order traversal , Please output the sequence of its sequence traversal . It is assumed that the key values are all positive integers which are not equal to each other .
Input format :
The first line of input gives a positive integer N(≤30), Is the number of nodes in a binary tree . The second line gives the sequence of traversal . The third line gives the order traversal sequence . Numbers are separated by spaces .
Output format :
Output the sequence traversal sequence of the tree in one line . Between the numbers 1 Space separation , There must be no extra space at the beginning and 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
analysis
In this problem, we need to know how to build a tree when we know the middle order traversal and post order traversal of a binary tree , Here we can do this , The last node of subsequent traversal must be the root node , Then find the node in the middle order traversal , According to the characteristics of the middle order traversal, the left and right subtrees can be separated , Then in recursive left subtree and with subtree , Do the same thing .
Then there is program traversal , It's directly used here bfs Search it .
Reference code :
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 10010;
int pos, n;
int pre[N], in[N], post[N];
struct node
{
int l, r, w;
}T[N];
// Build up trees
void creat(int inl, int inr, int u)
{
if(inl > inr) return ;
// puts("hh");
// cout << u << endl;
T[u].w = post[pos--];
T[u].l = 2 * u, T[u].r = 2 * u + 1;
int mid;
for(mid = inl; mid <= inr; mid++)
if(T[u].w == in[mid]) break;
// Must recurse right subtree first , Because some points in front of the root node of subsequent traversal must be points of the right subtree ,
// And we are looking for recursion according to the root section of subsequent traversal moving forward gradually
creat(mid + 1, inr, 2 * u + 1);
creat(inl, mid - 1, 2 * u);
}
// Sequence traversal
void bfs()
{
int idx = 0;
queue<int> q;
q.push(1);
while(q.size())
{
node t = T[q.front()];
q.pop();
if(!idx) cout << t.w;
else cout << " " << t.w;
idx++;
if(T[t.l].w != -1) q.push(t.l);
if(T[t.r].w != -1) q.push(t.r);
}
}
int main()
{
cin >> n;
rep(i, 1, n) cin >> post[i];
rep(i, 1, n) cin >> in[i];
memset(T, -1, sizeof T);
pos = n;
creat(1, n, 1);
bfs();
cout << endl;
return 0;
}
边栏推荐
- ES6 promise object
- Learn winpwn (3) -- sEH from scratch
- Summary of numpy installation problems
- How to set up voice recognition on the computer with shortcut keys
- [Blue Bridge Cup 2017 preliminary] grid division
- Codeforces Round #753 (Div. 3)
- Mtcnn face detection
- Machine learning notes week02 convolutional neural network
- Learning question 1:127.0.0.1 refused our visit
- AcWing 1294.樱花 题解
猜你喜欢

QT creator custom build process

Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis

Software testing and quality learning notes 3 -- white box testing

Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'

Neo4j installation tutorial
![[number theory] divisor](/img/ec/036d7e76cc566c08d336444f2898e1.jpg)
[number theory] divisor
![[Blue Bridge Cup 2017 preliminary] grid division](/img/e9/e49556d0867840148a60ff4906f78e.png)
[Blue Bridge Cup 2017 preliminary] grid division
![[free setup] asp Net online course selection system design and Implementation (source code +lunwen)](/img/ac/b518796a92d00615cd374c0c835f38.jpg)
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)

How to build a new project for keil5mdk (with super detailed drawings)

Machine learning -- census data analysis
随机推荐
QT creator shape
vs2019 使用向导生成一个MFC应用程序
快来走进JVM吧
Remember a company interview question: merge ordered arrays
Some notes of MySQL
ES6 let 和 const 命令
Did you forget to register or load this tag 报错解决方法
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
QT creator create button
Solution to the practice set of ladder race LV1 (all)
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
MySQL and C language connection (vs2019 version)
人脸识别 face_recognition
Reading BMP file with C language
Install mongdb tutorial and redis tutorial under Windows
What does BSP mean
vs2019 桌面程序快速入门
Niuke novice monthly race 40
JDBC principle