UVA 536 Tree Recovery

Source: Internet
Author: User

Original question:
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly
Looking binary trees with capital letters in the nodes.
This is a example of one of her creations:

To record hers trees for the future generations, she wrote down the strings for each tree:a preorder
Traversal (root, left subtree, right subtree) and a inorder traversal (left subtree, root, right subtree).
For the tree drawn above the preorder traversal are DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later
(But she never tried it).
Now, years later, looking again at the strings, she realized this reconstructing the trees was indeed
Possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned off to be tedious.
So now she asks you to write a program this does the job for her!
Input
The input file is contain one or more test cases.
Each test case consists of one line containing the strings ' Preord ' and ' Inord ', representing the
Preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters.
(Thus they is not longer than characters.)
Input is terminated by end of file.
Output
For each test case, recover Valentine's binary tree and print one line containing the tree ' s postorder
Traversal (left subtree, right subtree, root).

Sample Input
DBACEGF ABCDEFG
Bcad Cbad

Sample Output
Acbfged
Cdab
English:
Give you a binary tree of the first order traversal and the middle sequence traversal, give the post-order traversal. (Very classic OH)

#include <bits/stdc++.h> using namespace std;
    typedef struct Node {struct node* lchild;
    struct node* rchild;
char data;
}node,*btree;
        void Build (Btree &t,string pre,string in) {if (Pre.length () ==0) {t=null;
    Return
    } Char root=pre[0];
    int Ind=in.find (root);
    String Lchild_in=in.substr (0,ind);
    String Rchild_in=in.substr (ind+1);
    int Lchild_len=lchild_in.length ();
    int Rchild_len=rchild_in.length ();
    String Lchild_pre=pre.substr (1,lchild_len);
    String Rchild_pre=pre.substr (1+lchild_len);
    t= (Btree) malloc (sizeof (node));
        if (t!=null) {t->data=root;
        Build (t->lchild,lchild_pre,lchild_in);
    Build (t->rchild,rchild_pre,rchild_in);
        }} void Post (Btree &t) {if (T!=null) {post (t->lchild);
        Post (T->rchild);
    cout<<t->data;
    }} int main () {Ios::sync_with_stdio (false);
    String p,i;
    Btree T; while (cin>>P>>i) {build (t,p,i);
        Post (t);
    cout<<endl;
} return 0;

 }

Non-pointer version

#include <bits/stdc++.h>
using namespace std;
void build (int n,char *p,char *i,char *b)
{
    if (n<=0)
        return;
    Char r=p[0];
    int IND=STRCHR (I,R)-I.;
    Build (ind,p+1,i,b);
    Build (N-ind-1,p+ind+1,i+ind+1,b+ind);
    b[n-1]=r;
}
int main ()
{
    Ios::sync_with_stdio (false);
    Char p[1000],i[1000],b[1000];
    while (Cin>>p>>i)
    {
        int n=strlen (p);
        Build (n,p,i,b);
        b[n]= ' + ';
        cout<<b<<endl;
    }
    return 0;
}

Answer:
In the small white book This problem is an example, inadvertently found it in the purple book, hurriedly do away ~

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.