zoukankan      html  css  js  c++  java
  • POJ 2255

     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 }

    关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。 

    技术网站地址: vmfor.com

  • 相关阅读:
    数组和字符串//寻找数组的中心索引
    队列&栈//钥匙和房间
    队列&栈//01 矩阵
    队列&栈//图像渲染
    队列&栈//字符串解码
    队列&栈//用队列实现栈
    队列&栈//二叉树的中序遍历
    队列&栈//目标和
    队列&栈//克隆图
    队列&栈//逆波兰表达式求值
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4566745.html
Copyright © 2011-2022 走看看