已知前序遍历和后序遍历,怎么求可能的中序遍历

如题所述

仅供参考,
int creat(BiTree &T, ElemType pre[],ElemType post[],int low_x,int high_x,int low_h,int high_h)
{//根据先序序列和后序序列建立二叉链表,先序序列和后序序列存于一维数组中,四个整型变量表示数组的范围,0号单元留空,函数返回可建立二叉树的数目
count=1;
if(low_x>high_x || low_h >high_h) {T==NULL;return count;}
if(low_x high_h])
{
T=new BiNode;
T->data=pre.elem[low_x];
}
if(low_x+1<=high_x || high_h-1 >= low_h)
{
if(pre [low_x+1] ! = post [high_h-1])
{
顺序查找pre [low_x+1]在后序序列的位置a;
顺序查找post [high_h-1]在先序序列的位置b;
creat(T->lchild,pre,post,low_x+1,b-1,low_h,a);
creat(T->rchild,pre,post,b,high_x,a+1,high_h-1);
}
else if(pre [low_x+1] = = post [high_h-1])
{
count*=2;
请选择建立左子树或右子树,左输入0,右输入1,用L_R表示
cin>>L_R;
if(L_R= =0)
{
creat(T->lchild,pre,post,low_x+1,high_x,low_h,high_h-1);
creat(T->rchild,pre,post,1,0,1,0);
else {
creat(T->lchild,pre,post,1,0,1,0);
creat(T->rchild,pre,post,low_x+1,high_x,low_h,high_h-1);
}
}
if (low_x+1> high_x || high_h-1 < low_h)
{
creat(T->lchild,pre,post,1,0,1,0);
creat(T->rchild,pre,post, 1,0,1,0);
}
}
温馨提示:答案为网友推荐,仅供参考