#include <iostream>
#include <string>
using namespace std;
struct Node
{
char val ;
Node *rc , *lc ;
} ;
Node* rebuild(string pre,string mid)
{
int i , len ;
Node *head = new Node() ;
head->val = pre[0] ;
//cout << pre<< " " << mid << endl ;
len = mid.length() ;
for(i=0;i<len;i++)
{
if(pre[0]==mid[i])
{
if(i!=0)
{
head->lc = rebuild(pre.substr(1,i),mid.substr(0,i));//зѓзгЪї
}
else{
head->lc = NULL ;
}
if(i!=len-1)
{
head->rc = rebuild(pre.substr(i+1,len-1-i),mid.substr(i+1,len-1-i));//гвзгЪї
}
else{
head->rc = NULL ;
}
}
}
return head ;
}
void after(Node *head)
{
if(head==NULL)
{
return ;
}
else{
if(head->lc!=NULL)
after(head->lc) ;
if(head->rc!=NULL)
after(head->rc) ;
cout << head->val << endl ;
}
}
int main()
{
string pre , mid ;
Node *head = NULL ;
while(cin>>pre>>mid)
{
Node * head ;
head = rebuild(pre,mid) ;
after(head) ;
}
return 0;
}
前序中序求后序
猜你喜欢
转载自blog.csdn.net/u010112268/article/details/81450590
今日推荐
周排行