List Leaves

  • 题目描述
    1327401-20190829105326836-388040648.png

  • 题目思路

1 树的建立可以使用静态链表法。
2 题目要求从上到下,从左到右的顺序,就是对树进行层序遍历,层序遍历需要用到队列这种数据结构。
3 题目的输出要求“行尾不能有多余的空格”,可以把要输出的节点放到一个数组里,然后循环输出节点和空格,到最后一个节点时,只输出节点即可。

  • C代码实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>

#define TMaxSize 10

typedef int ElementType;

int Leafnode[TMaxSize];

struct QNode
{
    int* Data;  //存储元素的数组
    int Front;  //队列的头指针
    int Rear;  //队列的尾指针
    int MaxSize;  //队列的最大容量
};

struct TNode
{
    ElementType elem;
    int Left;
    int Right;
}T1[TMaxSize];


//创建一个队列
struct QNode* CreateQueue(int MaxSize)
{
    struct QNode* Q = (struct QNode*)malloc(sizeof(struct QNode));
    Q->Data = (int *)malloc(MaxSize * sizeof(int));
    Q->Front = Q->Rear = 0;
    Q->MaxSize = MaxSize;
    return Q;
}

bool IsFull(struct QNode* Q)
{
    return ((Q->Rear + 1) % Q->MaxSize) == Q->Front;
}

//在队列尾插入一个元素
//参数 struct QNode* Q 要操作的队列
//     int x  待插入的元素
bool AddQ(struct QNode* Q, int x)
{
    if (IsFull(Q))  //判断队列是否为空
    {
        printf("队列满,不能再插入元素\n");
        return false;
    }
    else
    {
        Q->Rear = (Q->Rear + 1) % Q->MaxSize;
        Q->Data[Q->Rear] = x;
        return true;
    }
}

//判断队列是否为空
bool IsEmpty(struct QNode* Q)
{
    return (Q->Front == Q->Rear);
}
//在队列头部删除一个元素
int DeleteQ(struct QNode* Q)
{
    if (IsEmpty(Q))
    {
        printf("队列为空\n");
        return false;
    }
    else
    {
        Q->Front = (Q->Front + 1) % Q->MaxSize;
        return Q->Data[Q->Front];
    }

}

int BuildTree(struct TNode T[])
{
    int N;
    int check[TMaxSize];
    int i = 0;
    char cl, cr;
    int Root;
    scanf("%d",&N);
    getchar();
    if (N != 0)
    {
        for (i = 0;i < N;i++)
        {
            check[i] = 0;
        }
        for (i = 0;i < N;i++)
        {
            scanf("%c %c",&cl,&cr);
            getchar();
            T[i].elem = i;
            if (cl != '-')
            {
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            } 
            else
            {
                T[i].Left = -1;
            }
            if (cr != '-')
            {
                T[i].Right = cr - '0';
                check[T[i].Right] = 1;
            }
            else
            {
                T[i].Right= -1;
            }
        }

        for (i = 0;i < N;i++)
        {
            if (!check[i])
            {
                Root = i;
                break;
            }
        }
    }
    return Root;
}
//使用队列实现层序遍历
void Traversal(int Root)
{ 
    int T;
    int i = 0;
    int j = 0;
    struct QNode* Q = CreateQueue(100);
    AddQ(Q,Root);
    while (!IsEmpty(Q))
    {
        T = DeleteQ(Q);
        if (T1[T].Left == -1 && T1[T].Right == -1)
        {
            Leafnode[i] = T;
            i++;
        }
        if (T1[T].Left != -1)
        {
            AddQ(Q, T1[T1[T].Left].elem);
        }
        if (T1[T].Right != -1)
        {
            AddQ(Q, T1[T1[T].Right].elem);
        }
    }
    while (j < i - 1)
    {
        printf("%d ",Leafnode[j]);
        j++;
    }
    printf("%d", Leafnode[i-1]);
}

int main()
{
    int R1;
    R1 = BuildTree(T1);
    Traversal(R1);
    // system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/Manual-Linux/p/11428481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值