Binary tree traversal reconstruction ZCMU-4931

I learned about the establishment of a binary tree before (very simulated), and today I wrote a topic, knowing that the pre-order, middle-order and post-order, that is, the reconstruction of the binary tree


Question stem

ZCMU-4931
4931: Binary tree traversal

Time Limit: 1 Sec Memory Limit: 32 MB
Submit: 82 Solved: 60
[Submit][Status][Web Board]
Description

The definition of preorder, middle order and postorder traversal of binary tree:
preorder traversal: for any subtree, first visit the follower, then traverse its left subtree, and finally traverse its right subtree;
middle order traversal: for any subtree For a tree, first traverse its left subtree, then visit the root, and finally traverse its right subtree;
post-order traversal: For any subtree, first traverse its left subtree, then traverse its right subtree, and finally visit the root.
Given the pre-order traversal and middle-order traversal of a binary tree, find the subsequent traversal (hint: given pre-order traversal and middle-order traversal can uniquely determine post-order traversal).

Input

For the two strings, the length n is less than or equal to 26.
The first line is pre-order traversal, and the second line is in-order traversal.
The node names in the binary tree are expressed in capital letters: A, B, C... up to 26 nodes.

Output

There may be multiple sets of input samples. For each set of test samples,
one line is output, which is the string to be traversed in the subsequent sequence.

Sample Input

ABC
CBA
ABCDEFG
DCBAEFG

Sample Output

CBA
DCBGFEA

Algorithm explanation

I used other people’s blog pictures, and I feel that the writing is still very clear

https://blog.csdn.net/qq_33951180/article/details/72790549#commentBox

Now we come to a set of examples
Insert picture description here

Obviously, we can find the following by looking at the picture: the
first order 1 2 4 7 3 5 6 8 the
middle order 4 7 2 1 5 3 8 6

According to the theorem, knowing the pre-order and middle-order can find the post-order, we can realize the reconstruction of the binary tree like this
Insert picture description here

We know that the pre-order storage scheme is root->left->right,
so the first node must be the total root. Next, we look for root in the middle sequence, because the middle sequence is established by left-> root->right , So the left side of root is root's left subtree, and the right side is root's right tree.

Then use recursion to get the answer.
Let's implement it in code!


AC code

#include<string>
#include<iomanip>
#include<iostream>
#include<set>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#define m(x) memset(x,0,sizof x);
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn = 1e5+10;
int n;
string a,b;
void f(int i,int j){
    
    
    if(i>j)return;
    int k;n++;
    //在b数组中寻找root
    for(k=i;k<=j;k++)if(a[n]==b[k])break;
    //递归操作er
    f(i,k-1);
    f(k+1,j);
    cout << b[k];
}
int main(){
    
    
    while(cin >> a >> b){
    
    
        n = -1;
        f(0,a.size()-1);
        cout << endl;
    }
    return 0;
}


Similar topics (just add a vector and it's done)
ZCMU-1132

to sum up

A deeper summary of the knowledge of binary trees

Guess you like

Origin blog.csdn.net/DAVID3A/article/details/114603548