Binary Tree Reconstruction-(sequential traversal, middle sequence traversal, post-order traversal)

Source: Internet
Author: User

For a treeTwo fork TreeT, we can recursively define its first-order traversal, the middle sequence traversal, and the post-order traversal:

1. Ordinal traversal (preorder (t) = root node of T + preorder (Zuozi of T) + preorder (right subtree of T))

2, Middle sequence traversal (inorder (t) = inorder (t Zuozi) + t root node + inorder (right subtree of T))

3. Post-sequential traversal (postorder (t) = Postorder (Zuozi of T) + postorder (right subtree of T) + t root node)

Where the plus sign represents a string join operation. For example, the two-fork tree, the first-order traversal is DBACEGF, and the middle-order traversal is ABCDEFG

(the painting is slightly ugly, do not abandon)

Requirements: Enter the first sequence traversal and the middle sequence traversal of a tree, and output his post-order traversal

Sample input:

DBACEGF ABCDEFG

Bcad Cbad

Sample output:

Acbfged

Cdab

Code:

#include <stdio.h> #include <string.h> #define MAXN 10void Build (int n, char *s1, Char *s2, char *s) {
   
    if (n <= 0) return;    int p = STRCHR (s2,s1[0])-S2; Locate the root node in the middle sequence traversal in the location    build (p,s1+1,s2,s);     Recursive construction of the left sub-tree of the post-sequential traversal    build (n-p-1, s1+p+1,s2+p+1,s+p);//construction of the right subtree after the post-traversal    s[n-1] = s1[0];} int main () {    char s1[maxn],s2[maxn],ans[maxn];    while (scanf ("%s%s", S1,s2) ==2) {        int nlen = strlen (S1);        Build (Nlen,s1,s2,ans);        Ans[nlen] = ' + ';        Puts (ans);    }    return 0;}
   


Through this example, I believe there is a certain understanding of the traversal of the two-fork tree, I do not know if you have, anyway, I have.

Portal: two fork tree hierarchy traversal


Binary Tree Reconstruction-(sequential traversal, middle sequence traversal, post-order traversal)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.