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(' '); } }