当前位置:网站首页>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;
}
边栏推荐
- A way to calculate LNX
- 2023年深圳市绿色低碳产业扶持计划申报指南
- Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
- Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
- 1: Citation;
- 图嵌入Graph embedding学习笔记
- js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
- Is it safe for CICC fortune to open an account online?
- IC科普文:ECO的那些事儿
- 基础篇——配置文件解析
猜你喜欢
计算lnx的一种方式
零道云新UI设计中
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
.Net分布式事務及落地解决方案
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
leetcode刷题:二叉树10(完全二叉树的节点个数)
2023年深圳市绿色低碳产业扶持计划申报指南
Wechat applet regular expression extraction link
【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
随机推荐
Is it safe for Galaxy Securities to open an account online?
CVPR 2022 | 常见3D损坏和数据增强
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Unity editor extended UI control
leetcode刷题:二叉树18(最大二叉树)
ffplay文档[通俗易懂]
CTF reverse Foundation
Practical demonstration: how can the production research team efficiently build the requirements workflow?
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
leetcode刷题:二叉树16(路径总和)
Leetcode brush questions: binary tree 18 (largest binary tree)
零道云新UI设计中
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
DP: tree DP
关于BRAM IP复位的优先级
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
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
[C language] merge sort
图嵌入Graph embedding学习笔记
Debezium series: modify the source code to support drop foreign key if exists FK