当前位置:网站首页>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
 Insert picture description here
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;
}
原网站

版权声明
本文为[Handsome BIA]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140858183099.html