I wrote two C language programs with functions such as questions. If you have any questions, please leave a message !!!
Development Platform: Linux; VIM; gcc
We know the first order, the middle order, and the back order:
# Include <stdio. h>
# Include <string. h>
# Define max 20
Char STR [Max] = {'/0 '};
Int main ()
{
Void postorder (char * str1, char * str2 );
Char str1 [Max] = {'/0 '};
Char str2 [Max] = {'/0 '};
Int I;
Printf ("Please input the preorder:/N ");
Scanf ("% S % * C", str1 );
Printf ("Please input the inorder:/N ");
Scanf ("% S % * C", str2 );
Postorder (str1, str2 );
Printf ("The postorder is:/N ");
For (I = 0; STR [I] = '/0'; I ++ );
Puts (STR + I );
}
Void postorder (char * str1, char * str2)
{
Static int num = MAX-1;
Int I, J;
Char str3 [Max] = {'/0 '};
Char str4 [Max] = {'/0 '};
Char str5 [Max] = {'/0 '};
Char str6 [Max] = {'/0 '};
If (str1 [0] = '/0' | str2 [0] ='/0 ')
Return;
STR [num --] = str1 [0];
I = 0;
J = 0;
While (str2 [I]! = Str1 [0])
Str3 [J ++] = str2 [I ++];
I ++;
J = 0;
While (str2 [I]! = '/0 ')
Str4 [J ++] = str2 [I ++];
For (I = 0; I <strlen (str3); I ++)
Str5 [I] = str1 [I + 1];
For (j = 0; j <strlen (str4); j ++)
Str6 [J] = str1 [++ I];
Postorder (str6, str4 );
Postorder (str5, str3 );
}
After the known order, the middle order, the first order:
# Include <stdio. h>
# Include <string. h>
# Define max 20
Char STR [Max] = {'/0 '};
Int main ()
{
Void postorder (char * str1, char * str2 );
Char str1 [Max] = {'/0 '};
Char str2 [Max] = {'/0 '};
Int I;
Printf ("Please input the inorder:/N ");
Scanf ("% S % * C", str1 );
Printf ("Please input the postorder:/N ");
Scanf ("% S % * C", str2 );
Postorder (str1, str2 );
Printf ("the preorder is:/N ");
Puts (STR );
}
Void postorder (char * str1, char * str2)
{
Static num = 0;
Int I, j, position;
Char str3 [Max] = {'/0 '};
Char str4 [Max] = {'/0 '};
Char str5 [Max] = {'/0 '};
Char str6 [Max] = {'/0 '};
If (str1 [0] = '/0' | str2 [0] ='/0 ')
Return;
Position = strlen (str2)-1;
STR [num ++] = str2 [position];
I = 0;
J = 0;
While (str1 [I]! = Str2 [position])
Str3 [J ++] = str1 [I ++];
I ++;
J = 0;
While (str1 [I]! = '/0 ')
Str4 [J ++] = str1 [I ++];
For (I = 0; I <strlen (str3); I ++)
Str5 [I] = str2 [I];
For (j = 0; j <strlen (str4); j ++)
Str6 [J] = str2 [I ++];
Postorder (str3, str5 );
Postorder (str4, str6 );
}