中序+先序构造二叉树,后序遍历

#include<cstdlib>
#include<cstdio>
#include<cstring>

using namespace std;
char preorder[30];
char inorder[30];
int left[30];//相当于链表中指针的作用
int right[30];

int buildtree(int L1,int R1,int L2,int R2){
   // printf("*l %s *y%s",preorder,inorder);
    //if(L1>R1) return 0;//
    if(L1>R1) return -1;//因为0要代表我们的结点'A',不用作为空的标记,我们用-1表示空
    int p=L2;
    while(preorder[L1]!=inorder[p]) p++;
    int root=preorder[L1]-'A';

    int len=p-L2;
    left[root]=buildtree(L1+1,L1+len,L2,p-1);//递归构造左子树
   //right[root]=buildtree(R1-len+1,R1,p+1,R2);//递归构造右子树 错误的递归关系
    right[root]=buildtree(L1+1+len,R1,p+1,R2);//递归构造右子树
    //[(L1),L1+1,L1+len,L1+1+len,R1]
    //[L2,p-1,(p),p+1,R2]
}
void postra(int root){
   
    if(left[root]!=-1) postra(left[root]);
    if(right[root]!=-1) postra(right[root]);
    printf("%c",root+'A');

}
int main(){
    while(scanf("%s %s",preorder,inorder)!=EOF){
           // memset(left,99,sizeof(left));//memset只能赋值为0 或者 -1;
           // memset(right,99,sizeof(right));
            memset(left,-1,sizeof(left));//memset只能赋值为0 或者 -1;
            memset(right,-1,sizeof(right));
        int root=buildtree(0,strlen(preorder)-1,0,strlen(inorder)-1);//边界!!,否则递归会出错的
     //   printf("root=%d  %c\n",root,root+'A');
        //后序遍历
        //for(int i=0;i<30;i++) printf("left[%d]=%d , right[%d]=%d\n",i,left[i],i,right[i]);
        postra(root);
        printf("\n");

    }

return 0;
}

注意事项:

1、初始递归边界strlen()-1;

2、用-1表示空,因为0表示‘A’

3、memset只能初始化为0或者-1

4、正确的递归关系

[(L1),L1+1,L1+len,L1+1+len,R1]

[L2,p-1,(p),p+1,R2]

猜你喜欢

转载自www.cnblogs.com/debug-the-heart/p/12592566.html
今日推荐