当前位置:网站首页>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;
}
边栏推荐
- Truncated sentences of leetcode simple questions
- Market status and development prospect forecast of global button dropper industry in 2022
- Notes | numpy-11 Array operation
- [research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
- Leetcode simple question: check whether the array is sorted and rotated
- What is UUID
- data2vec! New milestone of unified mode
- Sdl2 + OpenGL glsl practice (Continued)
- [develop wechat applet local storage with uni app]
- RT thread flow notes I startup, schedule, thread
猜你喜欢
Number of 1 in binary (simple difficulty)
Mobile terminal - uniapp development record (public request encapsulation)
并发操作-内存交互操作
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
[research materials] annual report of China's pension market in 2021 - Download attached
【XSS绕过-防护策略】理解防护策略,更好的绕过
Handler understands the record
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
随机推荐
Wechat applet distance and map
Why does I start with =1? How does this code work?
Leetcode simple question: the key with the longest key duration
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Basic use of Metasploit penetration testing framework
Handler understands the record
Thesis reading_ Chinese medical model_ eHealth
Sdl2 + OpenGL glsl practice (Continued)
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
[USACO 2009 Dec S]Music Notes
The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
Shell script -- condition judgment
Shuttle + Alluxio 加速内存Shuffle起飞
Leetcode simple problem delete an element to strictly increment the array
Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
String matching: find a substring in a string
I stepped on a foundation pit today
MC Layer Target
SSM framework integration