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);
        }
    }
    */
  • 相关阅读:
    【笔记】黄如花.信息检索.学习心得
    【心得】Lattice和Xilinx工具关键特性对比(Diamond、ISE)
    【导航】FPGA相关
    【笔记】黄如花.信息检索.前4章心得(新增大牛汇总的公开课资源)
    python正则表达式练习题
    python正则表达式(1)--特殊字符
    【转】什么时候 i = i + 1 并不等于 i += 1?
    Linux查看文件指定行数内容
    python mysqldb批量执行语句executemany
    linux命令行常用快捷键
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11729453.html
Copyright © 2011-2022 走看看