yycyxl117 2024-11-28 18:22 采纳率: 60%
浏览 8
已结题

C语言二叉树遇到NULL之后,后面的内容都没有被读取

请编写程序,根据给定二叉树的前序序列化结果,重构二叉树,并输出其前序遍历结果。

输入格式:
输入首先给出一个不超过 20 的正整数 n,随后一行给出 n 个前序序列的元素。其中键值都是不超过 9 位的正整数,空结点对应符号 #。

输出格式:
输出二叉树的前序遍历结果,每个数字占一行。

根据题目,我写出代码如下

#include<stdio.h>
#include<stdlib.h>

typedef struct node {
    int data;
    struct node* lchild;
    struct node* rchild;
}node;

node* CreateTree(node* tr)
{
    int num;

    int right = scanf("%d", &num);
    if (right != 0)
    {
        tr = (node*)malloc(sizeof(node));
        if (tr != NULL)
        {
            tr->data = num;
            //tr->lchild = NULL;
            //tr->rchild = NULL;
            tr->lchild = CreateTree(tr->lchild);
            tr->rchild = CreateTree(tr->rchild);
        }
        return tr;
    }
    else return NULL;
}
void PrintTree(node* tr)
{
    if (tr != NULL)
    {
        printf("%d\n", tr->data);
        PrintTree(tr->lchild);
        PrintTree(tr->rchild);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    node* tr=(node*)malloc(sizeof(node));

    tr=CreateTree(tr);
    PrintTree(tr);
    return 0;
}

运行例子
11
1 2 # 4 # # 3 5 # # #
得到的二叉树只有
1
2
为什么#后面的内容全部没有读入呢,我尝试了下不输入#就可以正常创建二叉树,但是一输入#后面的所有内容都没有被读取了
求解答,谢谢!

  • 写回答

2条回答 默认 最新

  • micthis 2024-11-28 20:04
    关注

    当right等于0时应该清除当前的#,否则下次又会读到#,改成这样:

    #include<stdio.h>
    #include<stdlib.h>
    typedef struct node {
        int data;
        struct node* lchild;
        struct node* rchild;
    }node;
    node* CreateTree(node* tr)
    {
        int num;
        int right = scanf("%d", &num);
        if (right != 0)
        {
            tr = (node*)malloc(sizeof(node));
            if (tr != NULL)
            {
                tr->data = num;
                //tr->lchild = NULL;
                //tr->rchild = NULL;
                tr->lchild = CreateTree(tr->lchild);
                tr->rchild = CreateTree(tr->rchild);
            }
            return tr;
        }
        else
        {
            scanf("%*s");
            return NULL;
        }
    }
    void PrintTree(node* tr)
    {
        if (tr != NULL)
        {
            printf("%d\n", tr->data);
            PrintTree(tr->lchild);
            PrintTree(tr->rchild);
        }
    }
    int main()
    {
        //int n;
        //scanf("%d",&n);
        node* tr=(node*)malloc(sizeof(node));
        tr=CreateTree(tr);
        PrintTree(tr);
        return 0;
    }
    

    还有CreateTree参数多余,可以删除,进一步可以改成这样:

    #include<stdio.h>
    #include<stdlib.h>
    typedef struct node {
        int data;
        struct node* lchild;
        struct node* rchild;
    }node;
    node* CreateTree()
    {
        int num;
        int right = scanf("%d", &num);
        if (right != 0)
        {
            node* tr = (node*)malloc(sizeof(node));
            if (tr != NULL)
            {
                tr->data = num;
                //tr->lchild = NULL;
                //tr->rchild = NULL;
                tr->lchild = CreateTree();
                tr->rchild = CreateTree();
            }
            return tr;
        }
        else
        {
            scanf("%*s");
            return NULL;
        }
    }
    void PrintTree(node* tr)
    {
        if (tr != NULL)
        {
            printf("%d\n", tr->data);
            PrintTree(tr->lchild);
            PrintTree(tr->rchild);
        }
    }
    int main()
    {
        node* tr;
        tr=CreateTree(tr);
        PrintTree(tr);
        return 0;
    }
    当然,按理还应该释放二叉树,就不写了。
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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