• 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("
    ");
    }
    
    
    
    
    
    
    
  • 相关阅读:
    vmware里面的名词 vSphere、vCenter Server、ESXI、vSphere Client
    SQL Server2014 SP2新增的数据库克隆功能
    看完SQL Server 2014 Q/A答疑集锦:想不升级都难!
    Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项
    最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目
    基于本地存储的kvm虚拟机在线迁移
    SQL Server 数据加密功能解析
    android开发之GestureDetector手势识别(调节音量、亮度、快进和后退)
    Datazen介绍
    jquery智能弹出层,自己主动推断位置
  • 原文地址:https://www.cnblogs.com/Dup4/p/9433171.html
Copyright © 2020-2023  润新知