zoukankan      html  css  js  c++  java
  • 03-树2 List Leaves(25 point(s)) 【Tree】

    03-树2 List Leaves(25 point(s))

    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

    思路

    广度优先搜索 往下搜 遇到 没有儿子的 压入 一个 vector 就可以

    AC代码

    #include <cstdio>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cmath>
    #include <climits>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <map>
    #include <stack>
    #include <set>
    #include <numeric>
    #include <sstream>
    #include <iomanip>
    #include <limits>
    
    #define CLR(a) memset(a, 0, sizeof(a))
    #define pb push_back
    
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    typedef pair <int, int> pii;
    typedef pair <ll, ll> pll;
    typedef pair<string, int> psi;
    typedef pair<string, string> pss;
    
    const double PI = 3.14159265358979323846264338327;
    const double E = exp(1);
    const double eps = 1e-30;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 1e5 + 5;
    const int MOD = 1e9 + 7;
    
    struct Node
    {
        int l, r;
    }q[10];
    
    queue <int> Q;
    
    vector <int> ans;
    
    void bfs()
    {
        int len = Q.size();
        for (int i = 0; i < len; i++)
        {
            int num = Q.front();
            Q.pop();
            if (q[num].l != -1 && q[num].r != -1)
            {
                Q.push(q[num].l);
                Q.push(q[num].r);
            }
            else if (q[num].l != -1)
                Q.push(q[num].l);
            else if (q[num].r != -1)
                Q.push(q[num].r);
            else
                ans.pb(num);
        }
        if (Q.size())
            bfs();
    }
    
    int main()
    {
        int n;
        scanf("%d", &n);
        char a, b;
        map <int, int> m;
        for (int i = 0; i < n; i++)
        {
            scanf(" %c %c", &a, &b);
            if (a != '-')
            {
                q[i].l = a - '0';
                m[a - '0'] = 1;
            }
            else
                q[i].l = -1;
            if (b != '-')
            {
                q[i].r = b - '0';
                m[b - '0'] = 1;
            }
            else
                q[i].r = -1;
        }
        int root;
        for (int i = 0; i < n; i++)
        {
            if (m[i] == 0)
            {
                root = i;
                break;
            }
        }
        Q.push(root);
        bfs();
        vector <int>::iterator it;
        for (it = ans.begin(); it != ans.end(); it++)
        {
            if (it != ans.begin())
                printf(" ");
            printf("%d", (*it));
        }
        printf("
    ");
    }
    
    
    
    
    
    
    
  • 相关阅读:
    Mac下安装Navicat12之破解
    重新认识spring
    冒泡排序(二)
    算法入门篇(一)
    elasticsearch复杂查询-----2
    idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found?
    idea 多模块项目依赖父工程class找不到问题
    apache-maven-3.3.9集成apache-tomcat-7.0.72实现热部署配置细节
    WebService CXF调试常见报错及解决方案
    通过dbcp链接池对数据库操作报 Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)--解决方案
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433171.html
Copyright © 2011-2022 走看看