当前位置:网站首页>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;
}
边栏推荐
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
- 8. Commodity management - commodity classification
- Bumblebee: build, deliver, and run ebpf programs smoothly like silk
- Introduce reflow & repaint, and how to optimize it?
- Hmi-31- [motion mode] solve the problem of picture display of music module
- February database ranking: how long can Oracle remain the first?
- Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
- LeetCode --- 1071. Great common divisor of strings problem solving Report
- Chinese natural language processing, medical, legal and other public data sets, sorting and sharing
猜你喜欢

8. Commodity management - commodity classification

Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers

Chinese natural language processing, medical, legal and other public data sets, sorting and sharing

1.五层网络模型

2021 Li Hongyi machine learning (1): basic concepts

Avoid material "minefields"! Play with super high conversion rate

看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事

腾讯云,实现图片上传

Tiny series rendering tutorial
![[technology development-26]: data security of new information and communication networks](/img/13/10c8bd340017c6516edef41cd3bf6f.png)
[technology development-26]: data security of new information and communication networks
随机推荐
2021 Li Hongyi machine learning (1): basic concepts
使用druid連接MySQL數據庫報類型錯誤
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
spoon插入更新oracle数据库,插了一部分提示报错Assertion botch: negative time
this+闭包+作用域 面试题
Moco V2 literature research [self supervised learning]
Asynchronous and promise
2021 Li Hongyi machine learning (2): pytorch
单项框 复选框
Azkaban实战
Single line function*
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
Comparison of advantages and disadvantages between platform entry and independent deployment
【LeetCode】404. Sum of left leaves (2 brushes of wrong questions)
【LeetCode】222. The number of nodes of a complete binary tree (2 mistakes)
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
Elfk deployment
2021 Li Hongyi machine learning (3): what if neural network training fails
2. Common request methods
Six stone programming: advantages of automated testing