1417 traversal and conversion

Source: Internet
Author: User

 

Description

For a binary tree, if we know its pre-order and mid-order traversal, we can launch its tree structure to know its post-order traversal. The middle and back order traversal are known. Likewise, the former order can be obtained.

Now you only need to be smart. You can use the pre-order and mid-order traversal to find the post-order traversal.

Input

Enter the first row integer N, indicating the number of data groups. The following 2n rows are N groups of data. Each row of Row 2 and 2nd k + 1 has a string stringleaf and stringsnow (0 <k <n + 1), indicating the pre-order traversal and Middle-order traversal of the tree, A string is only composed of letters 'a'-'Z.

Output

There are n rows in the output. Each row has one string, which is the corresponding post-order traversal.

Sample Input
2
ABC
BAC
ABDFCE
DBFACE
Sample output
BCA
DFBECA

Classic problem: Conversion in three traversal Modes

# Include <stdio. h> # include <string. h> int find (char C, char a [], int S, int e)/** // * locate the root position in the middle order. */{Int I; for (I = s; I <= E; I ++) if (a [I] = c) return I ;} /** // * pre [] indicates the starting position of the first order, pre_s indicates the starting position of the first order, and pre_e indicates the Ending position of the first order. * // ** // * Where in [] indicates the middle order, in_s indicates the starting position of the middle order, and in_e indicates the Ending position of the middle order. * // ** // * Pronum () to obtain pre [pre_s ~ Pre_e], in [in_s ~ In_e. */Void pronum (char pre [], int pre_s, int pre_e, char in [], int in_s, int in_e) {char C; int K; If (in_s> in_e) return;/** // * invalid subtree, complete. */If (in_s = in_e) {printf ("% C", in [in_s]); /** // * The Child tree is output and completed directly when it is only one node. */Return;} c = pre [pre_s];/** // * C stores the root node. */K = find (C, In, in_s, in_e);/** // locate the root node in the middle order. */Pronum (PRE, pre_s + 1, pre_s + k-in_s, In, in_s, k-1); // *** recursively solve the left subtree separated. */Pronum (PRE, pre_s + k-in_s + 1, pre_e, In, k + 1, in_e);/*** // * recursive solution splits the right subtree. */Printf ("% C", c);/** // * root node output. */} Main () {int number; char pre [1000]; char in [1000]; scanf ("% d", & number); While (number --) {scanf ("% s", & Pre); scanf ("% s", & in); pronum (PRE, 0, strlen (in)-1, in, 0, strlen (pre)-1); printf ("\ n ");}}

 

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.