Sequential traversal of binary tree pre-sequence traversal + middle sequence traversal

Source: Internet
Author: User

BNU basic problems, data structure of the basic problem, by the way.

Binary tree is a kind of common data structure. We can use uppercase letters to denote the nodes of a binary tree.

As follows:

    B      /  \      /    \                     C      A                              D

For binary tree, there are three kinds of traversal methods, which are pre-order, middle-order and post-sequence. Now give you a binary tree of the pre-order and the middle sequence traversal, please find the binary tree after the sequential traversal results.

Input

There are multiple groups of input data, one row per group of data.

Each row consists of two strings (each string has a maximum length of 26). Represents the pre-order and middle-sequence traversal results of a binary tree.

The title guarantees that the pre-order and the middle sequence traversal are legal (i.e. a binary tree can be identified).

Output

For each set of inputs, the output corresponds to the sequential traversal results of the two-fork tree.

Note: The input and output are in the console, using the standard input and output functions, no need to read and write files.

Sample Input
Bcad CBADABDGKLRVWSXCEHMNFIOTUJPYQZ Kgvrwlsxdbamhnectouifpyjzq
Sample Output
Cdabkvwrxslgdbmnhetuoiypzqjfca
Source2009 Beijing Normal University computer professional security Research second-round machine problem

I've never been able to write a question like this before, but I've never really understood the meaning of recursion by looking at someone else's code.

Code:

1#include <stdio.h>2#include <string.h>3 4 Chars1[ -], s2[ -];5 intCNT;6 7 voidTreeintLeftintRightCharch)8 {9      for(inti = left; i<right; i++){Ten         if(S2[i] = =ch) { One             if(Left <i) ATree (left, I, s1[cnt++]); -             if(i+1<Right ) -Tree (i+1, right, s1[cnt++]); theprintf"%c", ch); -              Break; -         } -     } + } -  +  A intMainvoid) at { -      while(SCANF ("%s%s", s1,s2)! =EOF) -     { -CNT =0; -Tree0, strlen (S1), s1[cnt++]); -printf"\ n"); in     } -     return 0; to}

 

Sequential traversal of binary tree pre-sequence traversal + middle sequence traversal

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.