• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Vigil

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

MOOC数据结构PTA-03-树2 List Leaves

题目表述

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
结尾无空行

题目理解

按顺序输出叶子节点的序号,仍是构建一个静态链表存储输入数据,然后build一个二叉树,用层序输出的方式,判断是否是叶子节点。

代码

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

#define element int
#define Maximum 10
#define NON -1
typedef struct node {
    element lchild, rchild;
    struct node* next;
} Node;
typedef struct queue {
    struct node *front, *rear;
} Queue;
//**********函数声明**********//
int buildTree(Node*);
void showLeaves(Node* tree, int Root);
Queue* Init_Queue();
bool isempty(Queue* queue);
void EnQueue(Queue* queue, Node* x);
bool DeQueue(Queue* queue, Node* x);
void visit(Node*, Queue*, Node*);

int main() {
    Node tree[Maximum];
    int Root = buildTree(tree);
    showLeaves(tree, Root);
    return 0;
}

int buildTree(Node* p) {
    int N;
    scanf("%d", &N);
    int* check = (int*)malloc(sizeof(int) * N);
    char left, right;
    for (int i = 0; i < N; i++)
        check[i] = 0;
    for (int i = 0; i < N; i++) {
        scanf(" %c %c", &left, &right);
        (p + i)->lchild = (left == '-') ? NON : (left - '0');
        (p + i)->rchild = (right == '-') ? NON : (right - '0');
        if (left != '-')
            check[(p + i)->lchild] = 1;
        if (right != '-')
            check[(p + i)->rchild] = 1;
    }
    int root = NON;
    for (int i = 0; i < N; i++) {
        if (check[i] == 0) {
            root = i;
            break;
        }
    }
    free(check);
    return root;
}
void showLeaves(Node* tree, int Root) {
    Queue* q = Init_Queue();
    EnQueue(q, tree + Root);
    Node* x = (Node*)malloc(sizeof(Node));
    while (!isempty(q)) {
        DeQueue(q, x);
        if (x->lchild != NON)
            EnQueue(q, tree + x->lchild);
        if (x->rchild != NON)
            EnQueue(q, tree + x->rchild);
        // printf("%d->", (x->next - tree));
        visit(tree, q, x);
    }
    free(x);
}
Queue* Init_Queue() {
    Queue* q = (Queue*)malloc(sizeof(Queue));
    q->front = (Node*)malloc(sizeof(Node));
    q->rear = q->front;
    q->front->next = NULL;
    return q;
}
bool isempty(Queue* queue) {
    if (queue->front == queue->rear)
        return true;
    else
        return false;
}
void EnQueue(Queue* queue, Node* x) {
    queue->rear->next = x;
    queue->rear = x;
}
bool DeQueue(Queue* queue, Node* x) {
    if (isempty(queue))
        return false;
    Node* temp = queue->front->next;
    x->lchild = temp->lchild;
    x->rchild = temp->rchild;
    x->next = temp;
    queue->front->next = temp->next;
    if (temp == queue->rear)
        queue->rear = queue->front;
    // free(temp);
    return true;
}
void visit(Node* tree, Queue* q, Node* x) {
    if (x->rchild == NON && x->lchild == NON) {
        printf("%d", x->next - tree);
        if (!isempty(q))
            putchar(' ');
    }
}

image

posted on 2021-08-02 22:20  VigilYang  阅读(55)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3