当前位置:网站首页>Informatics Orsay all in one 1339: [example 3-4] find the post order traversal | Valley p1827 [usaco3.4] American Heritage
Informatics Orsay all in one 1339: [example 3-4] find the post order traversal | Valley p1827 [usaco3.4] American Heritage
2022-07-05 20:18:00 【Jun Yi_ noip】
【 Topic link 】
ybt 1339:【 example 3-4】 To find the subsequent traversal
Luogu P1827 [USACO3.4] American blood American Heritage
Both questions are known preorder and middle order ergodic sequences , Find the postorder traversal sequence
The difference for :【ybt 1339】 First enter the preorder traversal sequence , Then enter the middle order traversal sequence .【 Luogu P1827】 First enter the middle order traversal sequence , Then enter the preorder traversal sequence .
【 Topic test site 】
1. Binary tree
We know the antecedent 、 Middle order side path sequence , Find the postorder traversal sequence
【 Their thinking 】
solution 1:
Suppose the input middle order traversal sequence is :DBAECH, The preorder traversal sequence is ABDCEF. Take this input as an example to discuss the solution of the problem .
The current problem to be solved is : We know the antecedent 、 Middle order traversal sequence , Building a binary tree .
- First, the first letter in the preorder traversal is the value of the root node of the tree (A).
- Then find the root node value in the middle order traversal sequence (A) The location of , The sequence to the left of the root value (DB) Is the middle order traversal sequence of the left subtree of the root node . The sequence to the right of the root value (ECF) Is the middle order traversal sequence of the right subtree of the root node .
- In the preorder traversal sequence, the length of the sequence traversed according to the order in the left subtree , Find the preorder traversal sequence of the left subtree (BD), And the preorder traversal sequence of the right subtree (CEF).
- Recursively call “ Known preorder in order traversal sequence to find binary tree ” Function of . Now we know the left subtree of the root node 、 Preorder of right subtree 、 Middle order traversal sequence , Then the left subtree and right subtree of the root node can be constructed ( Building subtrees is relative to building A A tree that is the root node , It's a small-scale problem , When solving large-scale problems recursively , The solution of the small-scale problem is considered to be known )
- With A Is the root node , Connect the constructed left and right subtrees , That is, the construction of the tree is completed .
Next, do post order traversal of this tree , You can get the post order traversal sequence of the tree .
How to write it 1: Use character array
Enter the sequence of first order traversal to s_pre, Middle order traversal sequence to character array s_in.
function createTree Need to ml,mr,pl,pr, Meaning for : By a priori sequence s_pre[pl]~s_pre[pr]
And middle order sequence s_in[ml]~s_in[mr]
Construct a binary tree
According to the above algorithm , First, find the first character of the sequence in the middle sequence s_pre[pl]
The location of , Find the location for i. Then the middle order traversal sequence of the left subtree is s_in[ml]~s_in[i-1]
, The length is i-ml, In the preorder traversal sequence , from pl+1 Start with the length i-ml Sequence , The position of the last element is pl+i-ml, Then the preorder traversal sequence of the left subtree is s_pre[pl+1]~s_pre[pl+i-ml]
. Similarly , The middle order sequence of the right subtree can be obtained as s_in[i+1]~s_in[mr]
, The precedence sequence is s_pre[pl+i-ml+1]~s_pre[pr]
.
Use createTree Generate left and right subtrees respectively , Next to the newly assigned root node , You get this tree .
The recursive exit is : The subscript range of preorder and middle order sequences must have pl<=pr
And ml<=mr
, If this condition is not met , Sequence range is meaningless , Should return to .
How to write it 2: Use string class
The idea is similar to the above method , I won't repeat .
Use string class , have access to substr Member function to get substring . The sequence of each incoming function 、 Middle order sequences are string Class object .
【 Solution code 】:ybt 1339:【 example 3-4】 To find the subsequent traversal
How to write it 1: Use character array
#include <bits/stdc++.h>
using namespace std;
#define N 1000
struct Node
{
char val;
int left, right;
};
Node node[N];
int p = 1;
char s_pre[105], s_in[105];//s_pre: First, we traverse the sequence s_in: Middle order traversal sequence
// By a priori sequence s_pre[pl]~s_pre[pr] And middle order sequence s_in[ml]~s_in[mr] Construct a binary tree , Return to root node
int createTree(int pl, int pr, int ml, int mr)
{
if(pl > pr || ml > mr)
return 0;
int np = p++, i;
node[np].val = s_pre[pl];
for(i = ml; i <= mr; ++i)
{
if(s_in[i] == s_pre[pl])
break;
}
node[np].left = createTree(pl + 1, pl + i - ml, ml, i - 1);
node[np].right = createTree(pl + i - ml + 1, pr, i + 1, mr);
return np;
}
void postOrder(int root)
{
if(root != 0)
{
postOrder(node[root].left);
postOrder(node[root].right);
cout << node[root].val;
}
}
int main()
{
cin >> s_pre >> s_in;
int root = createTree(0, strlen(s_pre) - 1, 0, strlen(s_in) - 1);
postOrder(root);
return 0;
}
How to write it 2: Use string class
#include <bits/stdc++.h>
using namespace std;
#define N 1000
struct Node
{
char val;
int left, right;
};
Node node[N];
int p = 1;
string s_pre, s_in;
int createTree(string sp, string si)// Using a preorder sequence sp And middle order sequence si Building a binary tree , Return to tree root
{
int np = p++, i;
node[np].val = sp[0];
for(i = 0; i < si.length(); ++i)
{
if(sp[0] == si[i])
break;
}
int len_l = i, len_r = si.length() - 1 - i;// The sequence length of left and right subtrees
if(len_l > 0)// The sequence length is greater than 0, To build a tree
node[np].left = createTree(sp.substr(1, len_l), si.substr(0, len_l));
if(len_r > 0)
node[np].right = createTree(sp.substr(i+1, len_r), si.substr(i+1, len_r));
return np;
}
void postOrder(int root)
{
if(root != 0)
{
postOrder(node[root].left);
postOrder(node[root].right);
cout << node[root].val;
}
}
int main()
{
cin >> s_pre >> s_in;
int root = createTree(s_pre, s_in);
postOrder(root);
return 0;
}
【 Solution code 】: Luogu P1827 [USACO3.4] American blood American Heritage
How to write it 1: Use character array
#include <bits/stdc++.h>
using namespace std;
#define N 1000
struct Node
{
char val;
int left, right;
};
Node node[N];
int p = 1;
char s_pre[105], s_in[105];//s_pre: First, we traverse the sequence s_in: Middle order traversal sequence
// By a priori sequence s_pre[pl]~s_pre[pr] And middle order sequence s_in[ml]~s_in[mr] Construct a binary tree , Return to root node
int createTree(int pl, int pr, int ml, int mr)
{
if(pl > pr || ml > mr)
return 0;
int np = p++, i;
node[np].val = s_pre[pl];
for(i = ml; i <= mr; ++i)
{
if(s_in[i] == s_pre[pl])
break;
}
node[np].left = createTree(pl + 1, pl + i - ml, ml, i - 1);
node[np].right = createTree(pl + i - ml + 1, pr, i + 1, mr);
return np;
}
void postOrder(int root)
{
if(root != 0)
{
postOrder(node[root].left);
postOrder(node[root].right);
cout << node[root].val;
}
}
int main()
{
cin >> s_in >> s_pre;
int root = createTree(0, strlen(s_pre) - 1, 0, strlen(s_in) - 1);
postOrder(root);
return 0;
}
How to write it 2: Use string class
#include <bits/stdc++.h>
using namespace std;
#define N 1000
struct Node
{
char val;
int left, right;
};
Node node[N];
int p = 1;
string s_pre, s_in;
int createTree(string sp, string si)// Using a preorder sequence sp And middle order sequence si Building a binary tree , Return to tree root
{
int np = p++, i;
node[np].val = sp[0];
for(i = 0; i < si.length(); ++i)
{
if(sp[0] == si[i])
break;
}
int len_l = i, len_r = si.length() - 1 - i;// The sequence length of left and right subtrees
if(len_l > 0)// The sequence length is greater than 0, To build a tree
node[np].left = createTree(sp.substr(1, len_l), si.substr(0, len_l));
if(len_r > 0)
node[np].right = createTree(sp.substr(i+1, len_r), si.substr(i+1, len_r));
return np;
}
void postOrder(int root)
{
if(root != 0)
{
postOrder(node[root].left);
postOrder(node[root].right);
cout << node[root].val;
}
}
int main()
{
cin >> s_in >> s_pre;
int root = createTree(s_pre, s_in);
postOrder(root);
return 0;
}
边栏推荐
- [C language] three implementations of quick sorting and optimization details
- E. Singhal and Numbers(质因数分解)
- leetcode刷题:二叉树11(平衡二叉树)
- 全国爱眼教育大会,2022第四届北京国际青少年眼健康产业展会
- [quick start to digital IC Verification] 8. Typical circuits in digital ICs and their corresponding Verilog description methods
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- 解决php无法将string转换为json的办法
- 鸿蒙系统控制LED的实现方法之经典
- Minimum commission for stock trading account opening, where to open an account with low commission? Is it safe to open an account on your mobile phone
- Leetcode brush question: binary tree 14 (sum of left leaves)
猜你喜欢
港股将迎“最牛十元店“,名创优品能借IPO突围?
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)
鸿蒙系统控制LED的实现方法之经典
Leetcode skimming: binary tree 16 (path sum)
Database logic processing function
实操演示:产研团队如何高效构建需求工作流?
leetcode刷题:二叉树10(完全二叉树的节点个数)
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
全国爱眼教育大会,2022第四届北京国际青少年眼健康产业展会
IC科普文:ECO的那些事儿
随机推荐
Bzoj 3747 poi2015 kinoman segment tree
【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
.Net分布式事务及落地解决方案
July 4, 2022 - July 10, 2022 (UE4 video tutorial MySQL)
C langue OJ obtenir PE, ACM démarrer OJ
ICTCLAS word Lucene 4.9 binding
19 Mongoose模块化
Minimum commission for stock trading account opening, where to open an account with low commission? Is it safe to open an account on your mobile phone
[quick start of Digital IC Verification] 6. Quick start of questasim (taking the design and verification of full adder as an example)
处理文件和目录名
2023年深圳市绿色低碳产业扶持计划申报指南
Station B up builds the world's first pure red stone neural network, pornographic detection based on deep learning action recognition, Chen Tianqi's course progress of machine science compilation MLC,
【c语言】快速排序的三种实现以及优化细节
leetcode刷题:二叉树10(完全二叉树的节点个数)
Leetcode(695)——岛屿的最大面积
USACO3.4 “破锣摇滚”乐队 Raucous Rockers - DP
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
- Oui. Net Distributed Transaction and Landing Solution
2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
2022年7月4日-2022年7月10日(ue4视频教程mysql)