当前位置:网站首页>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;
}
边栏推荐
- Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
- Azkaban概述
- Android advanced interview question record in 2022
- Asynchronous and promise
- Kotlin - 协程 Coroutine
- Pytest (4) - test case execution sequence
- openresty ngx_lua执行阶段
- Introduce reflow & repaint, and how to optimize it?
- Returns the lowest common ancestor of two nodes in a binary tree
- CAM Pytorch
猜你喜欢
Master Fur
[technology development-26]: data security of new information and communication networks
This + closure + scope interview question
Naacl 2021 | contrastive learning sweeping text clustering task
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
Cut! 39 year old Ali P9, saved 150million
【LeetCode】501. Mode in binary search tree (2 wrong questions)
qrcode:将文本生成二维码
Asynchronous and promise
Zabbix
随机推荐
Yyds dry goods inventory intelligent fan based on CC2530 design
Bert fine tuning skills experiment
LeetCode 314. Binary tree vertical order traversal - Binary Tree Series Question 6
Yolov5 model training and detection
100 basic multiple choice questions of C language (with answers) 04
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
Zabbix
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
ELK日志分析系统
Eight days of learning C language - while loop (embedded) (single chip microcomputer)
打破信息茧房-我主动获取信息的方法 -#3
[micro service SCG] 33 usages of filters
Elk log analysis system
February database ranking: how long can Oracle remain the first?
GFS distributed file system
Naacl 2021 | contrastive learning sweeping text clustering task
Azkaban安装部署
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time