zoukankan      html  css  js  c++  java
  • PAT003 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):
    #include <stdio.h>
    typedef struct listNode {
        
        int value;
        char leftIndex, rightIndex;
        struct listNode *left;
        struct listNode *right;
        struct listNode *next; // 队列用
        
    } ListNode;
    
    typedef struct queue {
        
        ListNode *front;
        ListNode *rear;
        
    } Queue;
    Queue *createQueue()
    {
        Queue *queue = (Queue *)malloc(sizeof(Queue));
        queue->front = NULL;
        queue->rear = NULL;
        return queue;
    }
    
    void addToQueue(Queue *queue, ListNode *node)
    {
        if (!(queue->rear)) {
            queue->rear = node;
        } else {
            queue->rear->next = node;
            queue->rear = node;
        }
        
        if (!(queue->front)) {
            queue->front = node;
        }
    }
    
    ListNode *deleteFromQueue(Queue *queue)
    {
        ListNode *temp = queue->front;
        if (temp) {
            queue->front = queue->front->next;
            return temp;
        } else {
            return NULL;
        }
    }
    
    int isEmptyQueue(Queue *queue)
    {
        if (queue->front == NULL) {
            return 1;
        } else {
            return 0;
        }
    }
    
    // List Leaves
    int main()
    {
        // 接受输入
        int nodeCount;
        scanf("%d", &nodeCount);
        
        ListNode a[nodeCount];
        int sum = 0;
        for (int i = 0; i < nodeCount; i++) {
            char leftIndex, rightIndex;
            getchar(); // 去除多余的 '
    '
            scanf("%c %c", &leftIndex, &rightIndex);
            ListNode input = *(ListNode *)malloc(sizeof(ListNode));
            input.leftIndex = leftIndex;
            input.rightIndex = rightIndex;
            input.left = NULL;
            input.right = NULL;
            input.value = i;
            a[i] = input;
            sum += (leftIndex == '-' ? 0 : leftIndex - '0') + (rightIndex == '-' ? 0 : rightIndex - '0');
        }
        
        // 建树
        for (int i = 0; i < nodeCount; i++) {
            ListNode *node = &(a[i]);
            char leftIndex = node->leftIndex;
            char rightIndex = node->rightIndex;
            node->left = leftIndex != '-' ? &(a[leftIndex - '0']) : NULL;
            node->right = rightIndex != '-' ? &(a[rightIndex - '0']) : NULL;
        }
        // 根节点下标
        int rootIndex = (nodeCount - 1) * nodeCount / 2 - sum;
        ListNode *root = &a[rootIndex];
        
        // 层次遍历 遇到叶节点输出
        Queue *queue = createQueue();
        addToQueue(queue, root);
        int flag = 1;
        while (!isEmptyQueue(queue)) {
            ListNode *node = deleteFromQueue(queue);
            if (!(node->left) && !(node->right)) {
                if (flag) {
                    printf("%d", node->value);
                    flag = 0;
                } else {
                    printf(" %d", node->value);
                }
            }
            if (node->left) {
                addToQueue(queue, node->left);
            }
            if (node->right) {
                addToQueue(queue, node->right);
            }
        }
    }

    运行结果:

  • 相关阅读:
    二分和牛顿法实现开根号
    leetcode 44. 通配符匹配
    leetcode 91. 解码方法
    leetcode 236. 二叉树的最近公共祖先
    leetcode 39. 组合总数
    leetcode:146. LRU缓存机制
    leetcode:124. 二叉树中的最大路径和
    二叉树前序遍历,中序遍历,后序遍历以及层次遍历实现
    leetcode: 4. 寻找两个有序数组的中位数
    Leetcode: 43. 接雨水
  • 原文地址:https://www.cnblogs.com/liufeng24/p/4394527.html
Copyright © 2011-2022 走看看