码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2255

时间:2015-06-10 18:43:20      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

POJ 2255

标签:

原文地址:http://www.cnblogs.com/gavinsp/p/4566745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!