#include<iostream>
using namespace std;
struct tree{
char a;
tree *lchild;
tree *rchild;
};
int i=0;
void preorder(tree *p,string a,string b,int x,int y)
{
if(i<b.length())
{
p->a=a[i];
p->lchild=new tree;
p->rchild=new tree;
//cout<<i<<"*"<<x<<"*"<<y<<endl;
int u=i;
if(b.find(a[i])-1!=x)
{
//cout<<"$$$";
preorder(p->lchild,a,b,x,b.find(a[i++]));
}
else
{
p->lchild=NULL;//cout<<"空";
}
if(b.find(a[u])+1!=y)
{
i++;
preorder(p->rchild,a,b,b.find(a[u]),y);//cout<<"空";
}
else
{
p->rchild=NULL;//cout<<"空";
}//cout<<i<<"*"<<x<<"*"<<y<<endl;
}
}
void postorder(tree *p)
{
if(p!=NULL)
{
postorder(p->lchild);
postorder(p->rchild);
cout<<p->a;
}
}
int main()
{
string a,b,c;
cin>>a>>b;
tree *p=new tree;
p->lchild=NULL;
p->rchild=NULL;
int y=b.length(),x=-1;
preorder(p,a,b,x,y);
postorder(p);
return 0;
}
x为左边界 y为右边界。 u是怕右孩子的i位置改变。
C++ 已知前序遍历,中序遍历 求后序遍历
最新推荐文章于 2025-03-06 15:10:19 发布