Poj 2255 Tree Recovery

Source: Internet
Author: User

Tree Recovery
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submissions: 9658 Accepted: 6067


Description

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 an example of one of her creations:

D

/\

/\

B E

/\\

/\\

A C G

/

/

F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree ). for the tree drawn abve the preorder traversal is dbacepidermal and the inorder traversal is ABCDEFG.
She thought that such a pair of strings wocould give enough information to reconstruct the tree later (but she never tried it ).

Now, years later, looking again at the strings, she realized that 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 out to be tedious.
So now she asks you to write a program that does the job for her!


Input

The input will contain in one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. both strings consist of unique capital letters. (Thus they are not longer than 26 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

Dbacepidermal ABCDEFG
BCAD CBAD
Sample Output

ACBFGED
CDAB
Source

Ulm Local 1997

 

/* General meaning: give you the pre-order traversal of the binary tree, and the middle-order traversal, find the next traversal, and 1710 on hdu is similar to this question is a single character, you can use strings to directly intercept */import java. util. optional; public class Main {// ACstatic String s; public static void main (String [] args) {empty input = new empty (System. in); while (input. hasNext () {String pre = input. next (); String rin = input. next (); Node node = BT (pre, rin); // create the root node PO (Node); System. out. println () ;}} private static void PO (Node node) {// traverse if (node! = Null) {PO (node. lchild); PO (node. rchild); System. out. print (node. data) ;}} private static Node BT (String pre, String rin) {if (pre. length () <= 0) return null; // Save the root node char gen = pre. charAt (0); Node gjd = new Node (gen); // Based on the root Node as the boundary, the central sequence is divided into the central sequence int index = rin of the left and right subtree. indexOf (gen); String lrin = rin. substring (0, index); String rrin = rin. substring (index + 1, rin. length (); // The length of the central sequence, which divides the forward sequence except the root node into the String lpre = pre of the left and right Subtrees. substring (1, lrin. length () + 1); String rpre = pre. substring (lrin. length () + 1, pre. length (); // recursive decomposition of left and right subtree gjd. lchild = BT (lpre, lrin); gjd. rchild = BT (rpre, rrin); return gjd ;}} class Node {// declare Binary Tree char data; Node lchild; Node rchild; Node (char data) {this. data = data; this. lchild = null; this. rchild = null ;}}

 

Related Article

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.