Time limit:1 second
Memory limit:32 MB
Special Judgment:No
Submit:31
Solution:19 tags
Description
Binary Tree pre-order, middle-order, and post-order traversal definitions:
Forward traversal: For any subtree, first access the heel, then traverse its left subtree, and finally traverse its right subtree;
In-order traversal: For any subtree, first traverse the left subtree, then access the root, and finally traverse the right subtree;
Post-order traversal: For any subtree, first traverse the left subtree, then traverse the right subtree, and finally access the root.
Specify the pre-order traversal and Middle-order traversal of a binary tree, and obtain the post-order traversal (Note: given the pre-order traversal and Middle-order traversal, the post-order traversal can be uniquely determined ).
Input Format
The length of N is equal to or less than 26.
First, first, and second.
The node names in the binary tree are represented by uppercase letters: A, B, C... a maximum of 26 nodes.
Output
There may be multiple groups of input samples. For each group of test samples,
Output a row, which is a string that is traversed in the descending order.
Sample Input
ABC
CBA
Abcdefg
Dcbaefg
Sample output
CBA
Dcbgfea
Code written based on the pen calculation method:
1 # include <stdio. h> 2 # include <string. h> 3 int postrun (char S1 [], char S2 []) 4 {5 Int Len; 6 Len = strlen (S1); 7 char ST1 [30], st2 [30]; // stores left and right subtree 8 If (LEN <= 1) 9 {10 printf ("% s", S1); // when there is only one value last, '\ 0' is still followed '. Therefore, the output string is still returned. In addition, when S1 is empty, it is also output. 11 return 0; 12} 13 int I, j, mid; 14 for (I = 0; I <Len; I ++) 15 {16 if (S1 [0] = S2 [I]) // find the position of the S1 root node in S2. in S2, the left is the left subtree, right: 17 break; 18} 19 mid = I; 20 for (I = 0; I <mid; I ++) 21 {22 ST1 [I] = S1 [I + 1]; 23 st2 [I] = S2 [I]; // two left subtree 24 25} 26 ST1 [I] = st2 [I] = '\ 0'; 27 postrun (ST1, st2); 28 for (I = 0; I <(len-mid-1); I ++) 29 {30 ST1 [I] = S1 [Mid + 1 + I]; 31 st2 [I] = S2 [Mid + 1 + I]; 32} 33 ST1 [I] = st2 [I] = '\ 0'; 34 postrun (ST1, st2); 35 printf ("% C", S1 [0]); 36 37 38 39} 40 int main (void) 41 {42 char str1 [30]; 43 char str2 [30]; 44 While (gets (str1) & gets (str2) 45 {46 postrun (str1, str2); 47 printf ("\ n "); 48} 49 50 return 0; 51}
Expand to search for the pre-order according to the sub-order:
Draw a graph to show the previous and subsequent steps: Root, left subtree, and right subtree distribution. The above code can be easily modified.
1 # include <stdio. h> 2 # include <string. h> 3 int postrun (char S1 [], char S2 []) // S1 is in the descending order, S2 is in the middle order 4 {5 Int Len; 6 Len = strlen (S1 ); 7 char ST1 [30], st2 [30]; // store left and right subtree 8 If (LEN <= 1) 9 {10 printf ("% s", S1 ); // when there is only one value at the end, '\ 0' is followed '. Therefore, the output string is still returned. In addition, when S1 is empty, it is also output. 11 return 0; 12} 13 int I, j, mid; 14 for (I = 0; I <Len; I ++) 15 {16 if (S1 [len-1] = S2 [I]) // find the position of the S1 root node in S2, in S2, the left is the left subtree, right: 17 break; 18} 19 mid = I; 20 for (I = 0; I <mid; I ++) 21 {22 ST1 [I] = S1 [I]; 23 st2 [I] = S2 [I]; // two left subtree 24 25} 26 ST1 [I] = st2 [I] = '\ 0'; 27 printf ("% C", S1 [len-1]); 28 postrun (ST1, st2); 29 for (I = 0; I <(len-mid-1); I ++) 30 {31 ST1 [I] = S1 [Mid + I]; 32 st2 [I] = S2 [Mid + 1 + I]; 33} 34 ST1 [I] = st2 [I] = '\ 0'; 35 postrun (ST1, st2); 36 37 38 39 40} 41 int main (void) 42 {43 char str1 [30]; 44 char str2 [30]; 45 while (gets (str1) & gets (str2) 46 {47 postrun (str1, str2); 48 printf ("\ n"); 49} 50 51 return 0; 52}
1910: binary tree traversal