当前位置:网站首页>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;
}
边栏推荐
- tuple and point
- 看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
- 【LeetCode】501. Mode in binary search tree (2 wrong questions)
- Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
- [daily problem insight] Li Kou - the 280th weekly match (I really didn't know it could be so simple to solve other people's problems)
- Tiny series rendering tutorial
- Devtools的简单使用
- [micro service SCG] 33 usages of filters
- February database ranking: how long can Oracle remain the first?
- Avoid material "minefields"! Play with super high conversion rate
猜你喜欢
1.五层网络模型
[technology development-26]: data security of new information and communication networks
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
Devtools的簡單使用
8. Commodity management - commodity classification
Naacl 2021 | contrastive learning sweeping text clustering task
1. Five layer network model
Avoid material "minefields"! Play with super high conversion rate
Design and implementation of kindergarten management system
Bumblebee: build, deliver, and run ebpf programs smoothly like silk
随机推荐
College Students' innovation project management system
LeetCode --- 1071. Great common divisor of strings problem solving Report
Apache build web host
Master Fur
Last week's hot review (2.7-2.13)
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Asynchronous and promise
Can you really learn 3DMAX modeling by self-study?
February database ranking: how long can Oracle remain the first?
How to find hot projects in 2022? Dena community project progress follow-up, there is always a dish for you (1)
Introduce reflow & repaint, and how to optimize it?
Marubeni Baidu applet detailed configuration tutorial, approved.
ELK日志分析系统
The database and recharge are gone
Yyds dry goods inventory intelligent fan based on CC2530 design
Qrcode: generate QR code from text
打破信息茧房-我主动获取信息的方法 -#3
Kotlin - 协程 Coroutine
Cut! 39 year old Ali P9, saved 150million
[200 opencv routines] 99 Modified alpha mean filter