第一次写,搞了一晚上,ac得那一瞬间泪流满面....建树的时候参数一定要是地址啊,千万不要带指针进去啊QAQ
#include<iostream>
#include<malloc.h>
using namespace std;
struct root
{
char num;
root* left;
root* right;
};
string zh,ho;
void build(root* &s,int as,int ae,int bs,int be)//建树
{
//s=(root*)malloc(sizeof(root));//节点
s=new root;
s->left=NULL;// 初
s->right=NULL;// 始
s->num=ho[be];// 化
int x=as;
while(zh[x]!=ho[be]){x++;}
if(x>as) build(s->left,as,x-1,bs,bs+x-as-1);//存在左子树;
if(x<ae) build(s->right,x+1,ae,be-ae+x,be-1);//存在右子树
}
void qi(root* s)//输出前序
{
if(s!=NULL)
{
cout<<s->num;
qi(s->left);
qi(s->right);
}
}
int main()
{
while(cin>>zh>>ho)
{
root *p;
build(p,0,zh.size()-1,0,ho.size()-1);
qi(p);
cout<<endl;
}
return 0;
}