PTA 03-树2 List Leaves 题目解析

20 篇文章 2 订阅
4 篇文章 0 订阅

PTA-mooc完整题目解析及AC代码库:PTA(拼题A)-浙江大学中国大学mooc数据结构全AC代码与题目解析(C语言)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a “-” will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6     
    

Sample Output:

4 1 5

题目分析

输出给定树的所有叶子结点,按从左到右、从上到小的顺序(类似层次遍历)

输入分析

第一行的正数N(N≤10)表示树的结点个数

之后的N行每一行都表示一个结点,结点信息有左子节点的编号和右子节点的编号,如果对应的子节点为空,则使用“-”表示

输出分析

在一行中按给定顺序输出所有叶子结点的编号

思路分析

同求解树的同构问题思路类似,整体步骤可分为:

  1. 二叉树的表示
  2. 建二叉树
  3. 输出所有叶子节点

二叉树表示采用结构体数组形式存储的静态链表,建立二叉树时可以按顺序存入至该数组对应位置即可,按照课程给定思路,可以遍历该数组找出没有被指向的结点索引即为头节点编号。

输出叶节点时,为满足要求的输出顺序,可使用层次遍历的方法,当遍历至叶子节点时输出即可。因为层次遍历使用到了队列,因此具体实现中也简单实现了一个队列。


Talk is cheap, show me the code.

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

#define MaxTree 10
#define Tree int
#define Null -1

struct TreeNode
{
    Tree Left;
    Tree Right;
} T[MaxTree];

#define MaxSize 11

struct QNode {
    Tree Data[MaxSize];
    int rear;
    int front;
};
typedef struct QNode *Queue;

Queue CreateQueue()
{
    Queue q = (Queue)malloc(sizeof(struct QNode));
    q->front = 0;
    q->rear = 0;
    return q;
}

void DestoryQueue(Queue q)
{
    free(q);
}

void AddQ(Queue PtrQ, Tree item)
{
    if ((PtrQ->rear+1) % MaxSize == PtrQ->front) return;
    PtrQ->rear = (PtrQ->rear + 1) % MaxSize;
    PtrQ->Data[PtrQ->rear] = item;
}

Tree DeleteQ(Queue PtrQ)
{
    if (PtrQ->front == PtrQ->rear) return NULL;
    else {
        PtrQ->front = (PtrQ->front + 1) % MaxSize;
        return PtrQ->Data[PtrQ->front];
    }
}

int IsEmpty(Queue Q)
{
    return Q->front == Q->rear ? 1 : 0;
}

Tree BuildTree()
{
    char cl, cr;
    int N, i;
    Tree Root;
    int check[MaxTree];
    Root = Null;
    scanf("%d", &N);
    if (N) {
        for (i = 0; i < N; ++i) check[i] = 0;
        for (i = 0; i < N; ++i) {
            scanf("\n%c %c", &cl, &cr);
            if (cl != '-') {
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            }
            else T[i].Left = Null;
            if (cr != '-') {
                T[i].Right = cr - '0';
                check[T[i].Right] = 1;
            }
            else T[i].Right = Null;
        }
        for (i = 0; i < N; ++i)
            if (!check[i]) break;
        Root = i;
    }
    return Root;
}

void ListLeaves(Tree R)
{
    Queue Q;    Tree tmp;
    int flag;
    if (R == Null) return;
    Q = CreateQueue();
    AddQ(Q, R);
    flag = 1;
    while (!IsEmpty(Q)) {
        tmp = DeleteQ(Q);
        if (T[tmp].Left == Null && T[tmp].Right == Null) {
            if (!flag) printf(" ");
            printf("%d", tmp);
            flag = 0;
        }
        if (T[tmp].Left != Null) AddQ(Q, T[tmp].Left);
        if (T[tmp].Right != Null) AddQ(Q, T[tmp].Right);
    }
}

int main()
{
    Tree R;
    R = BuildTree();
    ListLeaves(R);

    return 0;
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值