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("
    ");
    }
    
    
    
    
    
    
    
  • 相关阅读:
    25个完美的Opencart模板,让顾客无法抗拒!
    来自极客标签10款最新设计素材-系列十
    Creating Contextual Menus创建上下文菜单
    java解惑之常常忘记的事
    java 泛型实例详解(普通泛型、 通配符、 泛型接口)
    Java 泛型、通配符? 解惑
    Java中public,private,protected,和默认的区别
    windows 环境下dos 命令符下进D盘(非c盘系统盘)根目录
    I/O流之--转换流:InputStreamReader 和InputStreamWriter
    java 代码执行cmd 返回值异常 (关于JAVA Project.waitfor()返回值是1)
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433171.html
Copyright © 2011-2022 走看看