当前位置:网站首页>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;
}边栏推荐
- 论文阅读_清华ERNIE
- data2vec! New milestone of unified mode
- Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
- Source insight garbled code solution
- Uipath practice (08) - selector
- Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
- Contents of welder (primary) examination and welder (primary) examination in 2022
- 雇佣收银员(差分约束)
- Shell script -- condition judgment
- [research materials] annual report of China's pension market in 2021 - Download attached
猜你喜欢

The simple problem of leetcode: dismantling bombs
![[research materials] annual report of China's pension market in 2021 - Download attached](/img/24/622aeeb38de16ac84128b362ceeddb.jpg)
[research materials] annual report of China's pension market in 2021 - Download attached

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

Uipath practice (08) - selector

Learn to use the idea breakpoint debugging tool

The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping

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

Source insight garbled code solution

ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation

Truncated sentences of leetcode simple questions
随机推荐
Notes | numpy-08 Advanced index
论文阅读_ICD编码_MSMN
论文阅读_清华ERNIE
Hire cashier (differential constraint)
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
[XSS bypass - protection strategy] understand the protection strategy and better bypass
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
M1 Pro install redis
雇佣收银员(差分约束)
Market status and development prospect prediction of global fermented plant protein industry in 2022
document. The problem of missing parameters of referer is solved
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
Compile and decompile GCC common instructions
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
Introduction to JVM principle
Crazy scientist
Retirement plan fails, 64 year old programmer starts work again
Leetcode simple question: check whether the string is an array prefix
Notes | numpy-10 Iterative array