Binary Tree Traversal Binary Tree Derivation

Problem C: Binary tree traversal

topic

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

The length of the two strings 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 groups of input samples. For each group of test samples,
one line is output, which is the string to be traversed later.

Sample Input Copy

ABC
CBA
ABCDEFG
DCBAEFG
Sample Output Copy

CBA
DCBGFEA

Ideas

  • First derive the binary tree from the pre-order traversal and the middle-order traversal, and store it in the node
    • This question focuses on how to judge the root node and the left and right subtrees
    • The root node of each subtree is at the forefront of the preorder traversal, so the root node can be found and saved
    • The left subtree of each subtree is on the left side of the root node in the middle order traversal, so the left subtree can be found by finding the position of the root node in the middle order (note that the number of nodes is the same as the first order traversal)
    • The left subtree of each subtree is to the right of the root node in the middle order traversal
    • Therefore, the pre-order and middle-order sequences of the left and right subtrees of the node can be obtained, and recursion can be performed
  • Then through the post-order traversal (left and right middle order, using recursion (dfs)) output
  • As shown below (excerpt from algorithm notes)
    Insert picture description here

Code

#include <stdio.h>
#include <iostream>
using namespace std;

struct Node{
    
    
    char num;
    Node* left=NULL;
    Node* right=NULL;
};

string pre;
string in;

Node* resetTree(int preL,int preR,int inL,int inR){
    
    //注意不能把Node作为参数,否则会为null
    if(preL>preR||inL>inR)//注意判断是否这一侧子树已经不存在结点
        return NULL;
    Node* root=new Node;
    root->num=pre[preL];
    if(preL==preR)
        return root;
    int i=inL;
    for(i=inL;i<=inR;i++){
    
    
        if(pre[preL]==in[i])
            break;
    }
    
    root->left=resetTree(preL+1,preL+i-inL,inL,i-1);//左子树
    root->right=resetTree(preL+i-inL+1,preR,i+1,inR);//右子树
    return root;
}

void postorder(Node* root){
    
    
    if(root==NULL)
        return;
    postorder(root->left);
    postorder(root->right);
    printf("%c",root->num);
    return;
}

int main(){
    
    
    while(getline(cin,pre)){
    
    
        getline(cin,in);
        Node* root=resetTree(0,pre.length()-1,0,in.length()-1);
        postorder(root);
        printf("\n");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Cindy_00/article/details/109292834