5.3 Reconstruction of Binary Tree

5.3 Reconstruction of Binary Tree

table of Contents

5.3.1 Knowing post-order traversal and middle-order traversal seeking pre-order traversal

5.3.1 Knowing post-order traversal and middle-order traversal seeking pre-order traversal

First of all, a little bit of common sense, give you a post-order traversal, then the last one is the root (such as ABCD, the root is D).

Because the topic is ordered first, it means to constantly find the root.

Then let's look at the method of this question: (example)

In the middle sequence ACGDBHZKX, the latter sequence CDGAHXKZB, the main root B can be found first;

Then we find B in the in-order traversal. Based on the nature of this traversal, the in-order traversal can be divided into two subtrees, ACGD and HZKX,

Then the corresponding can be found after traversing CDGA and HXKZ (you can find it from the beginning)

Therefore, the problem becomes: 1. In order to traverse ACGD, after order to traverse the tree of CDGA 2. In order to traverse HZKX, and after order to traverse the tree of HXKZ;

Then recursively, according to the original method, find 1. Sub-root A, and then divide it into two sub-trees 2. Sub-root Z, and then divide it into two sub-trees.

Just keep doing it like this (output the root first, then recurse);

The template is summarized as step1: find the root and output

step2: Divide the middle sequence and the post sequence into two left and right subtrees;

step3: recursively, repeat step1,2;

P1030 [NOIP2001 Popularization Group] Seek first order

Title description

Give 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 \le 8≤8).

Input format

The two lines are all strings composed of uppercase letters, indicating the middle order and post order of a binary tree.

Output format

Line 1 indicates the preorder of a binary tree.

Sample input and output

enter

BADC
BDCA

Output

ABCD
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
void beford(string in,string after){
    
    
    if (in.size()>0){
    
    
        char ch=after[after.size()-1];
        cout<<ch;//找根输出
        int k=in.find(ch);
        beford(in.substr(0,k),after.substr(0,k));
        beford(in.substr(k+1),after.substr(k,in.size()-k-1));//递归左右子树;
    }
}
int main(){
    
    
    string inord,aftord;
    cin>>inord;cin>>aftord;//读入
    beford(inord,aftord);cout<<endl;
    return 0;
}

Reconstruction tree solution

#include <array>
#include <iostream>
#include <memory>
#include <string>
struct node
{
    
    
    char value;
    std::shared_ptr<node> l;//c++智能指针
    std::shared_ptr<node> r;//c++智能指针
    node(char value = '0', std::shared_ptr<node> l = nullptr, std::shared_ptr<node> r = nullptr):value(value), l(l), r(r) {
    
    };
};
std::array<char, 10> pre;
void buildtree(int, int, int&, std::shared_ptr<node>&, std::string, std::string);
void preorder(int&, std::shared_ptr<node>);
int main(void)
{
    
    
    int num, tmp;
    std::string in, post;
    std::shared_ptr<node> root;
    num = 0;
    pre.fill('0');
    std::cin >> in >> post;
    tmp = in.size() - 1;
    buildtree(0, in.size(), tmp, root, in, post);
    preorder(num, root);
    for (int i = 0; i < num; i++)
    {
    
    
        std::cout << pre.at(i);
    }
    std::cout << std::endl;
    return 0;
}
void buildtree(int left, int right, int& t, std::shared_ptr<node>& ns, std::string inStr, std::string postStr)
{
    
    
    int flag;
    flag = -1;
    for (int i = left; i < right; i++)
    {
    
    
        if (inStr.at(i) == postStr.at(t))
        {
    
    
            flag = i;
            i = right;
        }
    }
    if (-1 == flag)
    {
    
    
        return;
    }
    ns = std::make_shared<node>();
    ns->value = inStr.at(flag);
    --t;
    buildtree(flag + 1, right, t, ns->r, inStr, postStr);
    buildtree(left, flag, t, ns->l, inStr, postStr);
    return;
}
void preorder(int& n, std::shared_ptr<node> ns)
{
    
    
    if (ns != nullptr)
    {
    
    
        pre.at(n++) = ns->value;
        preorder(n, ns->l);
        preorder(n, ns->r);
    }
    return;
}

Guess you like

Origin blog.csdn.net/ZXG20000/article/details/113763634