当前位置:网站首页>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;
}
边栏推荐
- 2.14 summary
- The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
- @RequestMapping
- "Niuke brush Verilog" part II Verilog advanced challenge
- Wechat applet distance and map
- Valentine's day limited withdrawal guide: for one in 200 million of you
- Market status and development prospect prediction of global fermentation acid industry in 2022
- [luatos sensor] 1 light sensing bh1750
- Market status and development prospect prediction of global SoC Test Platform Industry in 2022
- Blog building tool recommendation (text book delivery)
猜你喜欢
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Games101 Lesson 9 shading 3 Notes
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Review the configuration of vscode to develop golang
Network security textual research recommendation
Kept hot standby and haproxy
On typescript and grammar
Sdl2 + OpenGL glsl practice (Continued)
[tools run SQL blind note]
Keepalived热备与HAProxy
随机推荐
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
Do you know UVs in modeling?
Market status and development prospect forecast of global button dropper industry in 2022
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
Review the old and know the new: Notes on Data Science
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
Leetcode simple question: check whether two string arrays are equal
Distinguish between releases and snapshots in nexus private library
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
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
Crazy scientist
I stepped on a foundation pit today
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
Learning practice: comprehensive application of cycle and branch structure (I)
Handler understands the record
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
General undergraduate college life pit avoidance Guide