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