永发信息网

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

答案:1  悬赏:80  手机版
解决时间 2021-04-10 04:36
  • 提问者网友:动次大次蹦擦擦
  • 2021-04-09 04:30
已知前序遍历和后序遍历,怎么求可能的中序遍历
最佳答案
  • 五星知识达人网友:由着我着迷
  • 2021-04-09 05:07
仅供参考,
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);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯