zoukankan      html  css  js  c++  java
  • 03-树2 List Leaves (25 分)

    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 (≤) 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
    //遍历方式是层次遍历
    #include<cstdio>
    #include<queue>
    using namespace std; 
    const int maxn = 15;
    struct Node
    {
        int left,right;
    }node[maxn];
    
    bool isRoot[maxn];
    int num = 0;
    
    void init();
    int change(char c);
    int FindRoot(int n);
    void BFS(int root);
    void print(int v);
    
    int main()
    {
        init();
        int n;
        char a,b;
        scanf("%d",&n);
        
        for (int i= 0; i < n; i++)
        {
            getchar();
            scanf("%c %c",&a,&b);
            node[i].left = change(a);
            node[i].right = change(b);
        }
        
        int root = FindRoot(n);
        BFS(root);
        return 0;
    }
    
    void init()
    {
        for (int i = 0; i < maxn; i++)
        {
            isRoot[i] = true;
        }
    }
    
    int change(char c)
    {
        int iRet = -1;
        if (c != '-')
        {
            iRet = c - '0';
            isRoot[iRet] = false;
        }
        return iRet;
    }
    
    int FindRoot(int n)
    {
        for (int i = 0; i < n; i++)
        {
            if (isRoot[i])
            {
                return i;
            }
        }
    }
    
    void BFS(int root)
    {
    #if 0    //使用数组模拟队列 
        int front = -1;  //
        int rear = -1;    //
        int queue[maxn] = {0};
        
        queue[++front] = root;
        while (front != rear)
        {
            int now = queue[++rear];
            if (node[now].left == -1 && node[now].right == -1)
            {
                print(now);
            }
            if (node[now].left != -1)
            {
                queue[++front] = node[now].left;    
            }
            if (node[now].right != -1)
            {
                queue[++front] = node[now].right;
            }        
        }
    #else    //使用c++的queue 
        queue<int> q;
        q.push(root);
        while (!q.empty())
        {
            int now = q.front();
            q.pop();
            if (node[now].left == -1 && node[now].right == -1)
            {
                print(now);
            }
            if (node[now].left != -1)
            {
                q.push(node[now].left);    
            }
            if (node[now].right != -1)
            {
                q.push(node[now].right);
            }
        }
    
    #endif
    }
    
    void print(int v)
    {
        if (0 == num)
        {
            printf("%d",v);
            num++;
        }
        else
        {
            printf(" %d",v);
        }
    }
    /*
    void BFS(int root)  先序遍历
    {
        if (node[root].left == -1 && node[root].right == -1)
        {
            if (1 == num)
            {
                printf("%d",root);
                num++;
            }
            else
            {
                printf(" %d",root);
            }
            return;
        }
        
        if (node[root].left != -1)
        {
            BFS(node[root].left);
        }
        if (node[root].right != -1)
        {
            BFS(node[root].right);
        }
    }
    */
  • 相关阅读:
    java环境变量配置 win7/win8 java配置
    (JSON转换)String与JSONObject、JSONArray、JAVA对象和List 的相互转换
    (yum)更新yum报错:yum makecache: error: argument timer: invalid choice: 'fast' (choose from 'timer')
    (ElasticSearch)中文字符串精确搜索 term 搜不到结果
    (端口)打开阿里云服务组端口和防火墙端口
    (乱码)Spring Boot配置文件出现乱码解决方案
    (特殊字符)url中包含特殊字符导致请求报错的解决方案
    (端口占用)Windows 查看所有端口和PID
    (注释)IDEA快捷键注释不能自动对齐
    (JDK)oracle下载jdk需要注册?
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11729453.html
Copyright © 2011-2022 走看看