Huas Summer training#2 D

Source: Internet
Author: User

Topic:

Description

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 Sub Tree, right subtree, root). The main idea: to give you the first sequence of the tree and the sequence traversal, you have to output post-order traversal. Problem-solving ideas: Give a structure with a tree root Saozi right child number. Use recursion to build up the tree. In the output. Code:
1#include <iostream>2#include <cstring>3 using namespacestd;4 Const intmaxn= -;5 structShu6 {7     CharA;8shu*l;9shu*R;Ten }B[MAXN]; Oneshu* Goujianshu (shu* D,Char* Q,Char* W,intN) A { -     inti; -     if(n==0) the         returnNULL; -      for(i=0; i<n;i++) -     { -         if(q[0]==W[i]) +         { -d->a=q[0]; +D->l=goujianshu (d+1, q+1, w,i); AD->r=goujianshu (d+1+i,q+i+1, w+i+1, n-i-1); at         } -     } -     returnD; - } - voidHouxu (shu*d) - { in     if(d==NULL) -         return; toHouxu (d->l); +Houxu (d->R); -cout<<Char(d->a); the } * intMain () $ {Panax Notoginseng     CharE[MAXN],T[MAXN]; -      while(cin>>e>>t) the     { + Goujianshu (B,e,t,strlen (e)); A Houxu (b); thecout<<Endl; +     } -     return 0; $}

Huas Summer training#2 D

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.