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("
    ");
    }
    
    
    
    
    
    
    
  • 相关阅读:
    excel导出字符串
    Unity shader学习之屏幕后期处理效果之Bloom效果
    Unity shader学习之屏幕后期处理效果之高斯模糊
    Unity shader学习之屏幕后期处理效果之均值模糊
    Unity之Vector3.SignedAngle实现
    Unity shader学习之屏幕后期处理效果之边缘检测
    Unity shader学习之屏幕后期效果之调整屏幕亮度,饱和度,对比度
    Unity shader学习之Grab Pass实现玻璃效果
    Unity shader学习之菲涅耳反射
    策略模式学习三---总结
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433171.html
Copyright © 2011-2022 走看看