当前位置:网站首页>Pat grade a 1119 pre- and post order traversals (30 points)
Pat grade a 1119 pre- and post order traversals (30 points)
2022-07-05 02:46:00 【Handsome BIA】
1119 Pre- and Post-order Traversals (30 branch )
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.
Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input 1:
7
1 2 3 4 6 7 5
2 6 7 4 5 3 1
Sample Output 1:
Yes
2 1 6 4 7 3 5
Sample Input 2:
4
1 2 3 4
2 4 3 1
Sample Output 2:
No
2 1 3 4
Thought analysis : It is also a familiar structural tree topic , I believe you have brushed other similar topics before , For example, build trees in the front and middle order , Or build a tree in the middle and later order . This question requires a pre order and a post order to build a tree , But we all know , The tree built without middle order may not be unique , Because in recursive divide and conquer , A node will appear on the left node , In the case that the right node is also true , That's the subject , In this case, we set the node on the right , Then use one flag Just mark it . Then let's talk about , How can this tree be divided and treated before it can be built .
First , For each root node in the preamble , Find the location of this root node in the post order and the second root node in the pre order , By this second node, the boundary is divided , In the following sequence , If the first node is on the right of the second node and there are elements between them , Note that the elements between them belong to the right subtree of the first node , The position of the second node and the previous elements belong to the left subtree of the first node .
Let's use the following example to explain :
Before the order :1 2 3 4 6 7 5
In the following order :2 6 7 4 5 3 1
1.1 In the following sequence index7,2 In the following sequence index1;
2.index7>index1 And there are elements between them , namely 7-1-1=5>0. therefore 2 yes 1 The left subtree ,6、7、4、5、3 Belong to 1 The right subtree ;
3.3 In the following sequence index6;
4.index1<index6, Not satisfied at this time 2 The position of is 3 To the right of , This is the explanation 3 Does not belong to 2 Is the tree of the root node , And belong to another subtree
The next step is to continue the above idea of recursion
Finally, attach the code
#include<bits/stdc++.h>
using namespace std;
const int N = 35;
int post[N],pre[N],n;
bool flag = 1;
typedef struct node *Tree;
struct node{
int data;
Tree l,r;
};
Tree BuildTree(int prel,int prer,int postl,int postr)
{
Tree root;
root = new node;
root -> data = pre[prel];
root -> l = root -> r = NULL;
if(prel == prer) return root;
int i = postl;
for (i = postl; post[i] != pre[prel + 1] ;i ++ );
if(postr - i > 1) // It can determine whether the node is on the left node or the right node
{
int len = i - postl + 1;
root -> l = BuildTree(prel + 1,prel + len,postl,i);
root -> r = BuildTree(prel + len + 1,prer,i + 1 , postr - 1);
}
else // Unable to determine which side the node is , Just hang the node on the right
{
flag = 0;
root -> r = BuildTree(prel + 1,prer,postl,postr - 1);
}
return root;
}
void inorderprint(Tree root)
{
if(!root) return ;
inorderprint(root -> l);
if(flag){
printf("%d", root -> data);
flag = 0;
}else printf(" %d", root -> data);
inorderprint(root -> r);
}
int main()
{
cin >> n;
for (int i = 1 ;i <= n; i++ ) cin >> pre[i];
for (int i = 1 ;i <= n; i++ ) cin >> post[i];
Tree root = BuildTree(1,n,1,n);
if(!flag) printf("No\n");
else printf("Yes\n");
flag = 1;
inorderprint(root);
cout<<endl;
return 0;
}
边栏推荐
- [200 opencv routines] 99 Modified alpha mean filter
- Open source SPL optimized report application coping endlessly
- 【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
- 2021 Li Hongyi machine learning (3): what if neural network training fails
- [micro service SCG] 33 usages of filters
- Day_ 17 IO stream file class
- Master Fur
- Pytest (4) - test case execution sequence
- Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
- Design and implementation of high availability website architecture
猜你喜欢
ELK日志分析系统
Design and implementation of kindergarten management system
2021 Li Hongyi machine learning (2): pytorch
Pytest (4) - test case execution sequence
Spark SQL learning bullet 2
Naacl 2021 | contrastive learning sweeping text clustering task
Zabbix
Bert fine tuning skills experiment
Qrcode: generate QR code from text
Apache build web host
随机推荐
Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
返回二叉树中两个节点的最低公共祖先
Port, domain name, protocol.
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
Zabbix
Can you really learn 3DMAX modeling by self-study?
Design of KTV intelligent dimming system based on MCU
Utilisation simple de devtools
打破信息茧房-我主动获取信息的方法 -#3
Design and practice of kubernetes cluster and application monitoring scheme
Application and Optimization Practice of redis in vivo push platform
Leetcode takes out the least number of magic beans
Devtools的簡單使用
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
Elk log analysis system
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
使用druid連接MySQL數據庫報類型錯誤
Android advanced interview question record in 2022
Hmi-32- [motion mode] add light panel and basic information column
Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it