曹嗲嗲今天敲代码了嘛 2021-12-11 01:40 采纳率: 66.7%
浏览 14
已结题

先序输入转中序遍历 不知道为什么输入没输出

/*
二叉树扩展先序输入转中序遍历
样例输入
abc##de#g##f###
样例输出
c b e g d f a
样例说明
根据给定的扩展先序遍历序列,建立对应的二叉树,然后对所得的二叉树进行中序遍历输出结果即可。

*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct BTnode{
char data;
struct BTnode* pLchild;
struct BTnode* pRchild;
}BTNODE,*PBTNODE;

void CreateBTree(PBTNODE*);
void Intraverse(PBTNODE);

int main(int argc, const char * argv[]) {

PBTNODE T;
CreateBTree(T);
Intraverse(T);
return 0;

}
void CreateBTree(PBTNODE *t){
char ch;
scanf("%c",&ch);
if(ch=='#') t=NULL;

else{
        *t=(PBTNODE)malloc(sizeof(BTNODE));
        (*t)->data=ch;//生成根节点
    CreateBTree(&(*t)->pRchild);//生成左子树
    CreateBTree(&(*t)->pLchild);//生成右子树
       
    }

}
void Intraverse(PBTNODE t){
if(t==NULL) return;
Intraverse(t->pLchild);
printf("%c",t->data);
Intraverse(t->pRchild);
}

  • 写回答

1条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct BTnode
    {
        char data;
        struct BTnode *pLchild;
        struct BTnode *pRchild;
    } BTNODE, *PBTNODE;
    
    void CreateBTree(PBTNODE *);
    void Intraverse(PBTNODE);
    
    int main(int argc, const char *argv[])
    {
        PBTNODE T;
        CreateBTree(&T);  // CreateBTree() 形参是二级指针, T前加& 传T的地址
        Intraverse(T);
        return 0;
    }
    void CreateBTree(PBTNODE *t)
    {
        char ch;
        scanf("%c", &ch);
        if (ch == '#')
            *t = NULL;  //  t前加 *
        else
        {
            *t = (PBTNODE)malloc(sizeof(BTNODE));
            (*t)->data = ch;             //生成根节点
            //先生成左子树再生成右子树
            CreateBTree(&(*t)->pLchild); //生成左子树
            CreateBTree(&(*t)->pRchild); //生成右子树
        }
    }
    void Intraverse(PBTNODE t)
    {
        if (t == NULL)
            return;
        Intraverse(t->pLchild);
        printf("%c", t->data);
        Intraverse(t->pRchild);
    }
    

    img

    如有帮助,望采纳!谢谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 已采纳回答 12月11日
  • 创建了问题 12月11日