Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12413 | Accepted: 7765 |
Description
D
/
/
B E
/ \
/ \
A C G
/
/
F
Input
Output
Sample Input
DBACEGF ABCDEFG BCAD CBAD
Sample Output
ACBFGED CDAB
Source
1 #include <stdio.h> 2 #include <cstring> 3 #include <queue> 4 #include <iostream> 5 using namespace std; 6 void findmid(string pre,string in){ 7 //cout<<"1 "<<pre<<" "<<in<<endl; 8 char c=pre[0];//根节点 9 //cout<<"1"<<c<<endl; 10 int i; 11 for(i=0;i<in.length();i++){ 12 if(c==in[i]){ 13 break; 14 } 15 } 16 if(i) //左子树不空 17 findmid(pre.substr(1,i),in.substr(0,i)); 18 if(i<in.length()-1)//右子树不空 19 findmid(pre.substr(i+1),in.substr(i+1)); 20 cout<<c; 21 } 22 int main(){ 23 //freopen("D:\\INPUT.txt","r",stdin); 24 string pre,in; 25 while(cin>>pre>>in){ 26 findmid(pre,in); 27 cout<<endl; 28 } 29 return 0; 30 }
原文:http://www.cnblogs.com/Deribs4/p/4641565.html