Algorithm training for first order arrangement (recursion, blue bridge cup C++, concise algorithm, code)

Algorithm training for prior order

Resource Limitation
Time Limitation: 1.0s Memory Limitation: 256.0MB
Problem Description
  gives the middle order and post order of a binary tree. Find its prior order. (It is agreed that tree nodes are represented by different capital letters, and the length is <=8).
Input format
  two lines, one character string in each line, respectively indicating middle order and post-order arrangement
Output format
  one character string, indicating the required order arrangement

Sample input
  BADC
  BDCA
sample output
ABCD

Foreword: Although this is a very basic question, I see an excellent idea, and I want to record it in the blog.

Ideas and analysis:

This is a question to find the first order for the middle order and the second order. Our general idea is to find the position of the same character in the first order as the last character (head node) in the latter order , print it out, and then put it on the left It is the left child, and the right child is on the right, and then recursively enter the string of the left child, find the root left and right , if there is no left child, return to find the right child, and find the right child string to the left child, recursive recursive recursive recursive...

1. For example, let’s take the
middle order in the example : BADC
post order: BDCA

First find the last character "A" in the post sequence in the middle sequence, find that its subscript is 1, and print A, because it is the root , then the post sequence is left with DBC, and the middle sequence is divided into the left substring "B "And the right substring "DC".

for(int i = 0;i < r1 ;i++)		//遍历中序中节点
    {
    
    
        if(a[i] == b[r2 - 1])	//找到和末尾一样的字符
        {
    
    
            printf("%c",a[i]);	//打印根
            k = i;				//记录下标
        }
    }

2. We recursively enter his left subtree and right subtree successively

 if( k > l1) tree(l1, k, l2, l2 + k - l1);
 if(k + 1 < r1) tree(k+1, r1,l2 + k - l1,r2 -1);

k is the coordinates of the root node and then the middle sequence. If the coordinates of the root node are not in the first position of the middle sequence, that is, k> l1, it means that he still has a left child, so continue to visit his left child. Then the critical coordinates of the left child should be l1 and k (here why is k instead of k-1, because in each traversal from i = 0; i <r1; i++, so i is <k), the right child The critical coordinates of is l2 and l2 + k-l1 , we know that the left child of the root node in the post sequence must start from the subscript 0, because it is a left and right root , then the left critical is no problem with l2, then the right critical is Count the number of left children from l2 to the right. For example, if the left child has only one B, it is to move one place, that is, l2 + k-l1, ( k-l1 represents the length of the left child before the root node ), then Until there is no left child, it will recurse and start recursive right child. Similarly , the four parameters k+1, r1,l2 + k-l1,r2 -1, I don’t think I need to talk about it. The next digit of k goes to the right. , And the left child starts on the right to before the last node index that has been printed.

So here has been a very detailed description of the recursive process, the complete code below:

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
 char a[10];
 char b[10];

int tree(int l1,int r1,int l2,int r2)//中序后序开始都是0到r1
{
    
    
	int k;
	for(int i = 0;i < r1 ;i++)
    {
    
    
        if(a[i] == b[r2 - 1])		//找到根
        {
    
    
            printf("%c",a[i]);		//打印根
            k = i;			//记录下标
        }
    }
    if( k > l1) tree(l1, k, l2, l2 + k - l1);	//如果有左孩子就一直递归
    if(k + 1 < r1) tree(k+1, r1,l2 + k - l1,r2 -1);		//左孩子递归完了后开始递归右孩子
}
int main()
{
    
    
    scanf("%s",a);
    scanf("%s",b);
    int lenth1 = strlen(a);			//length 是中序
    int lenth2 = strlen(b);			//length 是后序
    tree(0,lenth2,0,lenth2);
	return 0;
}


After we learn to give the middle and last counts the first order, we will try to give the middle first and the second order (of course, it is impossible to give the first and last middle order, because if there is no middle order you can not determine the position of the left and right children)

I also came up with a similar method based on the same idea :
our thoughts are still the same, first find the root node (the first one of the middle order traversal and the first order do ==)

for(int i = 0;i < r1 ;i++)
    {
    
    
        if(a[i] == b[l2])
        {
    
    
            k = i;
        }
    }

But it does not print if it finds it, but recursively its left and right substrings, until the bottom node has no children (neither left nor right) , print the node


	if( k > l1) tree(l1 , k, l2 + 1 , l2 + 1  + k - l1);//cout<<"1";
    if(k + 1 < r1) tree(k+1, r1,l2 + 1 + k - l1,r2);
    printf("%c",a[k]);

The left and right margins here, let's think about why for yourself, the blogger will not explain it anymore, and it is similar to the above;

Complete code:

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
 char a[10];
 char b[10];

int tree(int l1,int r1,int l2,int r2)
{
    
    
	int k;
	for(int i = 0;i < r1 ;i++)
    {
    
    
        if(a[i] == b[l2])
        {
    
    
            k = i;
        }
    }
    if( k > l1) tree(l1 , k, l2 + 1 , l2 + 1  + k - l1);//cout<<"1";
    if(k + 1 < r1) tree(k+1, r1,l2 + 1 + k - l1,r2);
     printf("%c",a[k]);
}
int main()
{
    
    
    scanf("%s",a);
    scanf("%s",b);
    int lenth1 = strlen(a);
    int lenth2 = strlen(b);
    tree(0,lenth2,0,lenth2);
	return 0;
}

Afterword: This question started after reading the article of the big guy, and I continued to derive the code for the first middle order and the second order. Today, I have learned a lot. If the article is open for discussion, I hope everyone will criticize and correct it!

Guess you like

Origin blog.csdn.net/Kyrie_irving_kun/article/details/113705519