浙大数据结构第三周之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

简单翻译:

就是给一把二叉树节点,其中要按从左往右,从上往下顺序依次输出所有叶子节点(强调:叶子节点的左右孩子都是空节点)

主要思路:

这题主要思路其实还好,也就是分为两部分,一部分是建树,与上一题一样,另一部分就是层序遍历,不过这里的队列数据结构是用c语言循环队列实现的,如果是特别要求逐层操作,往往考虑层序遍历,层序遍历顺序:

(1)从队列中取出一个元素

(2)访问该元素所指节点

(3)若该元素所指节点左右孩子非空,则将其左右孩子顺序入队

该题另一个要注意的点就是如何控制输出空格,定义了一个全局变量countLeave,先在建树时统计叶子结点数量,层序遍历每打印一个叶子节点就-1,打印最后一个就不会打印多余的空格

代码实现:

/*
第一部分:实现队列的数据结构
第二部分:实现读入节点原式数据,用结构体数组保存
第三部分:层序遍历
*/
#include 
#include 
#define MAX_SIZE 15
#define EMPTY '-'
#define NONE 14
/*实现队列数据结构*/
typedef struct QueueNode Queue;
struct QueueNode {
    int Data[MAX_SIZE];
    int Head, Rear;
    int NodeNum;
};
/*循环队列初始化*/
void Init(Queue* queue) {
    (*queue).Head = 0;
    (*queue).Rear = -1;
    (*queue).NodeNum = 0;
}
/*判断队列有没有满*/
bool IsFull(Queue* queue) {
    if((*queue).NodeNum == MAX_SIZE) return true;
    else return false;
}
/*判断队列是否为空*/
bool IsEmpty(Queue* queue) {
    if((*queue).NodeNum == 0) return true;
    else return false;
}
/*压入队列*/
void Push(Queue* queue, int num) {
    if(IsFull(queue)) return;
    (*queue).Data[++(*queue).Rear % MAX_SIZE] = num;
    (*queue).NodeNum++;
    return;
}
/*弹出队列*/
void Pop(Queue* queue) {
    if(IsEmpty(queue)) return;
    (*queue).Head = ((*queue).Head + 1) % MAX_SIZE;
    (*queue).NodeNum--;
    return;
}
/*返回队列队首元素*/
int Top(Queue* queue) {
    if(IsEmpty(queue)) return NONE;
    return (*queue).Data[(*queue).Head % MAX_SIZE];
}

/*实现读入树*/
typedef struct TreeNode Tree;
struct TreeNode {
    int LeftChild, RightChild;
    bool IsRoot;
};
Tree tree[MAX_SIZE];
int countLeave = 0;
int BuildTree() {
    /*初始化*/
    for(int i = 0; i < MAX_SIZE; i++) tree[i].IsRoot = true;
    int nodeNum;
    scanf("%d", &nodeNum);
    getchar();
    for(int i = 0; i < nodeNum; i++) {
        char tmpLeft, tmpRight;
        scanf("%c %c", &tmpLeft, &tmpRight);
        getchar();
        if(tmpLeft == EMPTY) {
            tree[i].LeftChild = NONE;
        }
        if(tmpLeft >= '0' && tmpLeft <= '9') {
            tree[i].LeftChild = tmpLeft - '0';
        }

        if(tmpRight == EMPTY) {
            tree[i].RightChild = NONE;
        }
        if(tmpRight >= '0' && tmpRight <= '9') {
            tree[i].RightChild = tmpRight - '0';
        }
        
        tree[tree[i].LeftChild].IsRoot = false;
        tree[tree[i].RightChild].IsRoot = false;
    }

    int rootId = NONE;
    for(int i = 0; i < nodeNum; i++) {
        if(tree[i].IsRoot == true) {
            rootId = i;
            break;
        }
    }
    for(int i = 0; i < nodeNum; i++) {
        if(tree[i].LeftChild == NONE && tree[i].RightChild == NONE) countLeave++;
    }
    return rootId;
}
void PrintLeave(int root) {
    Queue queue;
    Init(&queue);
    Push(&queue, root);
    while(!IsEmpty(&queue)) {
        int queueHead = Top(&queue);
        Pop(&queue);
        if(tree[queueHead].LeftChild == NONE && tree[queueHead].RightChild == NONE) {
            if(--countLeave) {
                printf("%d ", queueHead);
            }
            else {
                printf("%d", queueHead);
            }
        }
        if(tree[queueHead].LeftChild != NONE) {
            Push(&queue, tree[queueHead].LeftChild);
        }
        if(tree[queueHead].RightChild != NONE) {
            Push(&queue, tree[queueHead].RightChild);
        }
    }
    return;
}
int main() {
    int root = BuildTree();
    PrintLeave(root);
    return 0;
}

你可能感兴趣的:(数据结构,数据结构)