当前位置:网站首页>1119 pre- and post order traversals (30 points)
1119 pre- and post order traversals (30 points)
2022-07-03 04:54:00 【vs5】
The main idea of the topic : Given the pre order and post order, all over , To determine whether the middle order traversal is unique
Because when a node has only one child , Whether it's a left child or a right child , Their preorder traversal is the same , Post order traversal is the same .
A A
/ \
B B
\ /
C C
The first order of these two trees is ABC The following sequence is CBA, So we can't be sure about the children .
We know that pre order and post order cannot uniquely determine a tree , The preamble is about the root , The last order is left and right roots , We find the first node of preorder traversal preL It must be equal to the last node of post order traversal postR,preL+1 The left child. ( When the left child does not exist , It's the right child ),postR-1 It's the right child ( Empathy ),
If you find that preL+1 The node of is equal to postR-1 The node of , Then according to the nature of traversal , It can be either a left child or a right child , Here we can't be sure , If the title is not unique, just output one , So we regard him as both left and right . So we can find some in the preface , You can also find some in the following sequence , Then divide the left and right intervals , If you are looking for preL+1 The point of , We attribute this point to the left interval , If you find postR-1 The point of , We attribute this point to the right interval , It's easy to handle .
#include <iostream>
#include <vector>
using namespace std;
const int N = 110;
int pre[N],post[N],n,f;
vector<int>ans;
// The first value of the preceding sequence must be equal to the last value of the following sequence ,preL+1 Is the left node postR - 1 Is the right node
// When these two key When it's equal, you don't know whether it's a left node or a right node
/*
A A
/ \
B B
\ /
C C
The first order of these two trees is ABC The following sequence is CBA, So we can't be sure about the children
*/
void dfs(int preL,int preR,int postL,int postR)
{
if(preL > preR) return ;
if(preL == preR)
{
ans.push_back(pre[preL]);
return ;
}
int k = postL;
while(pre[preL + 1] != post[k] && k <= postR) k ++;// Find the position of the next point in the previous sequence
if(k == postR - 1) // postR - 1 == postL + 1
f = 1;// I'm not sure whether it's a left child or a right child
dfs(preL + 1,preL + k - postL + 1,postL,k);// Find the point of the left node in the post order traversal , We attribute this point to the left interval
ans.push_back(pre[preL]);// root
dfs(preL + k - postL + 2,preR,k + 1,postR - 1);
}
int main()
{
cin >> n;
for(int i = 0; i < n; i ++) cin >> pre[i];
for(int i = 0; i < n; i ++) cin >> post[i];
dfs(0,n - 1,0,n - 1);
if(f) puts("No");
else puts("Yes");
for(int i = 0; i < ans.size(); i ++)
{
if(i != 0) cout << ' ';
cout << ans[i];
}
puts("");
return 0;
}边栏推荐
- Silent authorization login and registration of wechat applet
- Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
- The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
- Learning record of arouter principle
- Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
- The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
- The process of browser accessing the website
- Realize file download through the tag of < a > and customize the file name
- Thesis reading_ Chinese medical model_ eHealth
- Market status and development prospect prediction of the global fire alarm sensor industry in 2022
猜你喜欢

LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)

《牛客刷verilog》Part II Verilog进阶挑战

Contents of welder (primary) examination and welder (primary) examination in 2022

Leetcode simple question: the key with the longest key duration
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750

RT thread flow notes I startup, schedule, thread

Silent authorization login and registration of wechat applet

【XSS绕过-防护策略】理解防护策略,更好的绕过

【工具跑SQL盲注】

Uipath practice (08) - selector
随机推荐
String matching: find a substring in a string
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Contents of welder (primary) examination and welder (primary) examination in 2022
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
论文阅读_ICD编码_MSMN
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
data2vec! New milestone of unified mode
Market status and development prospect forecast of global button dropper industry in 2022
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
Sprintf formatter abnormal exit problem
Leetcode simple question: check whether the string is an array prefix
Hire cashier (differential constraint)
M1 Pro install redis
Learning practice: comprehensive application of cycle and branch structure (I)
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
联发科技2023届提前批IC笔试(题目)
论文阅读_中文医疗模型_ eHealth
Thesis reading_ ICD code_ MSMN
Games101 Lesson 9 shading 3 Notes