1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 struct node 6 { 7 char c; 8 node * l; 9 node * r; 10 }; 11 12 node * make_tree(string pre,string ins); 13 14 void fun(node * root); 15 16 int main() 17 { 18 //freopen("acm.acm","r",stdin); 19 string pre; 20 string ins; 21 node * root; 22 while(cin>>pre>>ins) 23 { 24 // cout<<pre<<" "<<ins<<endl; 25 root = new node; 26 root = make_tree(pre,ins); 27 fun(root); 28 cout<<endl; 29 } 30 } 31 32 node * make_tree(string pre,string ins) 33 { 34 // cout<<" ------------ pre "<<pre<<"----------------- ins "<<ins<<endl; 35 node * root; 36 int index; 37 if(pre.length() > 0) 38 { 39 root = new node; 40 root->c = pre[0]; 41 index = ins.find(pre[0]); 42 root->l = make_tree(pre.substr(1,index),ins.substr(0,index)); 43 root->r = make_tree(pre.substr(index+1),ins.substr(index+1)); 44 } 45 else 46 root = NULL; 47 return root; 48 } 49 50 void fun(node * root) 51 { 52 if(root->l) 53 { 54 fun(root->l); 55 } 56 if(root->r) 57 { 58 fun(root->r); 59 } 60 cout<<root->c; 61 }
时间: 2024-10-09 17:46:55