# UVa 536 Tree Recovery (First order, middle order)

Source: Internet
Author: User

Test instructions give you the sequence of order sequence and order sequence of binary tree

The first order sequence must be the root of the sequence of the root of the left side of the tree belongs to the right of the right side of the tree is a recursive contribution to the child.

`#include <bits/stdc++.h>using namespace std;typedef struct tnode{    char data;    Tnode *LC, *RC;} node, *btree;void build (BTree &t, string Pre, string in) {    t = new node;    T->LC = T->RC = NULL, t->data = pre[0];    int k = In.find (pre[0]);    if (k > 0) Build (T->LC, Pre.substr (1, k), In.substr (0, k));    if (K < In.size ()-1) Build (T->RC, Pre.substr (k + 1), In.substr (k + 1));} void Posttral (BTree &t) {    if (!t) return;    Posttral (T->LC);    Posttral (T->RC);    Putchar (t->data);} int main () {    string pre, in;    while (CIN >> pre >> in)    {        BTree t;        Build (t, pre, in);        Posttral (t);        Puts ("");    }    return 0;}`

 Tree Recovery

Little Valentine liked playing with binary trees very much. Her favorite game is constructing randomly looking binary trees with capital letters in the nodes.

This is a example of one of her creations:

`                                    D                                   /                                   /                                    B     E                                /\                                    /   \     \                               A     C     G                                         /                                        /                                       F`

To record hers trees for the future generations, she wrote down the strings for each tree:a preorder traversal (root, left Sub Tree, 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 I T).

Now, years later, looking again in the strings, she realized that reconstructing the trees is indeed possible, but only B Ecause 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 SpecificationThe 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 Inord ER 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 Specificationfor 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 Abcdefgbcad Cbad`

Sample Output
`Acbfgedcdab`

UVa 536 Tree Recovery (First order, middle order)

Related Keywords:

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.