poj 2255

来源:互联网 发布:手机拨打电话软件 编辑:程序博客网 时间:2024/06/04 18:24

递归,学会了两个string的函数

substr(),第二个参数是长度;

代码如下

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;struct Node{char data;Node *lchild,*rchild;};Node *create(string pre,string in){Node *root;root=NULL;if(pre.length()>0){root=new Node;root->data=pre[0];int index=in.find(root->data);root->lchild=create(pre.substr(1,index),in.substr(0,index));          root->rchild=create(pre.substr(index+1),in.substr(index+1));  }return root;}void postorder(Node *&root)  {      if(root!=NULL)      {          postorder(root->lchild);          postorder(root->rchild);          cout<<root->data;      }  }  int main(){string pre,in;while(cin>>pre>>in){Node *root;          root=create(pre,in);          postorder(root);          cout<<endl; }return 0; } 


原创粉丝点击