Title: Post-order traversal time limit: milliseconds ms single point of time limit: Ms memory limit: MB description
After joining the Food Festival, Xiao hi and Xiao ho played in other places for a while. In this process, little ho got a very interesting toy-a binary tree connected by a ball and a wooden stick!
Little ho loves this binary tree, so he marks a label for each of its nodes-an uppercase letter belonging to a. Z, and no two nodes have the same label. Xiaohi also took a look at this opportunity and consolidated xiaoho's basic knowledge about binary tree traversal ~ In this way, it takes two days to complete.
On this day, Mr. Ho was just solving the results of the forward, central, and backward traversal of the binary tree, but accidentally threw the binary tree to the ground after finding the first and middle order traversal, parts such as ball and wooden stick are scattered all over the ground!
Mr. Ho lost his beloved toys and was about to burst into tears. Fortunately, Mr. Hi found out and persuaded him: "Don't worry, isn't this part still there? Isn't it ?"
"But I forgot what the binary tree looks like !" Little ho was frustrated.
"This is simple. Didn't you just find the results of the forward and middle order traversal of this binary tree? You can use these two information to restore the entire binary tree !"
"Is that true ?!!" Xiao ho stopped his tears and asked, "What should I do ?"
That's right! Mr. Ho encountered the following problems during the week:Returns the result of the forward and middle traversal of a binary tree, restores the binary tree, and outputs the result of the subsequent traversal.
Tip: Divide and conquer-convert to small, and then to non-Input
Each test point (input file) has only one set of test data.
The first behavior of each group of test data is a string consisting of uppercase letters, which indicates the result of the forward traversal of the binary tree.
The second behavior of each group of test data is a string consisting of uppercase letters, which indicates the result of the Middle-order traversal of the binary tree.
For 100% of the data, the number of nodes meeting the binary tree is less than or equal to 26.
Output
For each group of test data, a string consisting of uppercase English letters is output, indicating the result of the restored post-sequential traversal of the binary tree.
-
Sample Input
-
ABBA
-
Sample output
-
BA
// Forward, and backward # include <stdio. h> # include <string. h> void build (INT Len, char * S1, char * S2, char * s) {int Pos; int I; If (LEN <= 0) return; for (I = 0; I <Len; I ++) {If (s2 [I] = S1 [0]) {pos = I; // break ;}} build (Pos, S1 + 1, S2, S); Build (len-pos-1, S1 + POS + 1, S2 + POS + 1, S + POS ); s [len-1] = S1 [0];} int main () {char S1 [100]; char S2 [100]; char s [100]; int Len; while (scanf ("% s", S1 )! = EOF) {memset (S, '\ 0', sizeof (s); scanf ("% * C"); scanf ("% s", S2 ); len = strlen (S1); Build (Len, S1, S2, S); puts (s);} return 0 ;}
Hihocoder (Week 10) Recursive Implementation of Binary Trees (followed by forward and forward)