Frequently Asked Questions about Pre-In-Post-Order Traversal of Binary Trees

Dear readers: I am very happy that my article can be read, but it is not easy to create and edit, so please indicate the source of this article and attach the hyperlink to the address of this article and the blogger's blog address: https://blog. csdn.net/vensmallzeng . If you think this article is useful to you, please give a like and encouragement. The author would like to thank every reader here. If you need to contact the author, please write down your email address: [email protected] , thank you for your cooperation!

 

 

 

Recently, whether it is a written test or an interview, I often encounter such questions: "Given the preorder traversal and inorder traversal of a binary tree, please return its postorder traversal", "Given the postorder traversal of a binary tree Traversal and inorder traversal, please return its preorder traversal". In order not to make mistakes later, let's summarize it well.

 

1. Given that the preorder traversal of a binary tree is "ABCDEFGH" and the inorder traversal "BDCEAFHG", please return its postorder traversal?

The principle of pre-order traversal is "root-left-right", so the first node of pre-order traversal must be the root node, and then the root node is found in in-order traversal, and the left side of the root node is the left subtree of the binary tree. All nodes, the right side is all the nodes of the right subtree of the binary tree, and then the preorder traversal and the inorder traversal are split into two parts respectively to form the preorder traversal and inorder traversal of the two subtrees, that is, the left subtree preorder: BCDE, mid-order: BDCE, right subtree pre-order: FGH, mid-order: FHG, which can be solved by recursive method.

The problem solving code is as follows:

#include<iostream>
#include<string>

using namespace std;
//递归实现后序遍历
void findpostorder(string s1,string s2){
    if(s1.length()==0) return;
    if(s1.length()==1){
        printf("%c",s1[0]);
        return;
    }
    //访问索引(从0开始)
    int pos=s2.find(s1[0]);
    // 构成左子树的前序遍历与中序遍历
    findpostorder(s1.substr(1,pos),s2.substr(0,pos));//后序遍历左子树
    // 构成右子树的前序遍历与中序遍历
    findpostorder(s1.substr(pos+1,s1.length()-pos-1),s2.substr(pos+1,s2.length()-pos-1));
    // 此处输出则为前序遍历
    printf("%c",s1[0]);
}


int main(){
    string s1,s2;
    while(cin>>s1>>s2){
        // 输入二叉树的前序遍历与中序遍历
        findpostorder(s1,s2);
        printf("\n");
    }
}

 

2. Given that the post-order traversal of the binary tree is "DECBHGFA" and the in-order traversal "BDCEAFHG", please return its pre-order traversal?

#include<iostream>
#include<string>

using namespace std;
//递归实现前序遍历
void findpreorder(string s1,string s2){
    if(s1.length()==0) return;
    if(s1.length()==1){
        printf("%c",s1[s1.length()-1]);
        return;
    }
    //访问索引(从0开始)
    int pos=s2.find(s1[s1.length()-1]);
    // 此处输出则为前序遍历
    printf("%c",s1[s1.length()-1]);
    // 构成左子树的后序遍历与中序遍历
    findpreorder(s1.substr(0,pos),s2.substr(0,pos));
    // 构成右子树的后序遍历与中序遍历
    findpreorder(s1.substr(pos,s1.length()-pos-1),s2.substr(pos+1,s2.length()-pos-1));

}


int main(){
    string s1,s2;
    while(cin>>s1>>s2){
        // 输入二叉树的后序遍历与中序遍历
        findpreorder(s1,s2);
        printf("\n");
    }
}

 

 

 

 Over time, progress with you, add a summary, to be continued.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215187&siteId=291194637