Tsinghua machine trial: There is a forward, middle, and back order output.

Source: Internet
Author: User

 

 

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:

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:
ABCBACFDXEAGXDEFAG
Sample output:
BCAXEDGAF
Package Tsinghua;

Import java. Io. bufferedinputstream;
Import java. util. collections;

Public class evaluate the descending order traversal {
Static string str1, str2;
Public static void main (string [] ARGs ){
S = new second (New bufferedinputstream (system. In ));
While (S. hasnext ()){
Str1 = S. Next ();
Str2 = S. Next ();
Node root = creattree (str1, str2 );
Lastsee (Root );
System. Out. println ();
}

}

Static void lastsee (node tree ){
If (tree = NULL) return;
Lastsee (tree. L );
Lastsee (tree. R );
System. Out. Print (tree. data );
}
Static node creattree (string S1, string S2 ){
Node n = NULL;
If (S1 = NULL | S2 = NULL)
Return NULL;
String left2 = NULL, Right2 = NULL, news1 = NULL, news2 = NULL;
Int len1 = 0;
Char root = s1.charat (0 );
Int Index = s2.indexof (Root );
If (index> 0 ){
Left2 = s2.substring (0, index );
Len1 = left2.length ();
}
If (index <s2.length ()-1)
Right2 = s2.substring (index + 1 );
News1 = s1.substring (1, 1 + len1 );
News2 = s1.substring (1 + len1 );
N = new node (creattree (news1, left2), creattree (news2, Right2), s1.charat (0 ));
Return N;
}
}

Class node {
Node L;
Node R;
Char data;
Public node (node l, node R, char data ){
This. L = L;
This. r = R;
This. Data = data;
}

}

 

 

There are also methods that do not need to be built, copy others' code, and learn.

char a[30],b[30];
void Bice(int pf,int pr,int mp,int mr)
{
if(pf>pr)
return;
int i;
char c=a[pf];
for(i=0;i<=mr-mp;i++)
if(b[mp+i]==c)
break;
Bice(pf+1,pf+i,mp,mp+i-1);
Bice(pf+i+1,pr,mp+i+1,mr);
cout<<c;
}
int main()
{
while(cin>>a>>b)
{
int n=strlen(a);
Bice(0,n-1,0,n-1);
cout<<endl;
}
}
Related Article

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.