• 树2 List Leaves


    题目: https://pintia.cn/problem-sets/1268384564738605056/problems/1274008636207132673

    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 N1. 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


    题解: https://cloud.tencent.com/developer/article/1390933
    代码:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    struct Tree
    {
        char rchild, lchild;
        void clear()
        {
            rchild = 0;
            lchild = 0;
        }
    };
    
    Tree head[15];
    int q[15];  //模拟队列 
    
    int findRoot(Tree head[], int n)
    {
        int isRoot[15];  //相当于布尔数组 
        char left, right;
        memset(isRoot, 0, sizeof(isRoot));
        for(int i = 0; i < n; i++)
        {
            left = head[i].lchild - '0';
            if(left >= 0)
                isRoot[left] = 1;
            right = head[i].rchild - '0';
            if(right >= 0)
                isRoot[right] = 1;
        }
        for(int i = 0; i < n; i++)
        {
            if(isRoot[i] == 0)
                return i;
        }
    }
    
    void bfs(Tree head[], int n, int root)
    {
        int first = 1;//判断是不是第一个输出 
        int rear = 0, front = 0;//队列首尾 
        q[rear++] = root;
        while(front != rear)//当队列不为空 
        {
            if(head[q[front]].lchild != '-')
            {
                q[rear] = head[q[front]].lchild - '0';
                rear++;
            }
            if(head[q[front]].rchild != '-')
            {
                q[rear] = head[q[front]].rchild - '0';
                rear++;
            }
            if(head[q[front]].lchild == '-' && head[q[front]].rchild == '-')
            {
                if(first) 
                {
                    cout << q[front];
                    first = 0;
                }
                else
                    cout << " " << q[front];
            } 
            front++;
        }
    }
    
    int main()
    {
    //    freopen("listLeaves.txt", "r", stdin);
        int n;
        int root;
        char c, d;
        while(scanf("%d", &n) != EOF)
        {
            for(int i = 0; i < n; i++)
                head[i].clear();
            memset(q, 0, sizeof(q));
            for(int i = 0; i < n; i++)
            {
                cin >> head[i].lchild >> head[i].rchild;
            }
            root = findRoot(head, n);
            bfs(head, n, root);
        }
        return 0;
    }
  • 相关阅读:
    挂载nfs提示:mount.nfs: access denied by server while mounting...
    linux后台运行、关闭、查看后台任务常用命令
    linux sort命令用法
    tcping端口检测工具使用
    实现对MySQL数据库进行分库/分表备份(shell脚本)
    win server服务器 关闭危险端口 135,137,138,139,445的方法
    curl: (7) couldn't connect to host 解决方法
    vim调试Shell脚本: unexpected EOF while looking for matching
    sed:-e 表达式 #1,字符 10:未终止的“s”命令
    linux E325: 注意 发现交换文件 "*.swp" 解决方法
  • 原文地址:https://www.cnblogs.com/simon-chou/p/13619897.html
Copyright © 2020-2023  润新知