美文网首页
甲级-1004 Counting Leaves (30 分)

甲级-1004 Counting Leaves (30 分)

作者: 梦终无痕_311d | 来源:发表于2019-09-27 17:00 被阅读0次

    题目:

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:

    ID K ID[1] ID[2] ... ID[K]
    where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

    The input ends with N being 0. That case must NOT be processed.

    Output Specification:

    For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

    The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

    Sample Input:

    2 1
    01 1 02

    Sample Output:

    0 1

    解题思路:

    用左孩子右兄弟的形式构建二叉树。
    输出的时候使用栈或队列,依次遍历每个层次,同时统计每层的叶子结点数量。
    缺点是为了快速访问到每个结点使用 vector 保存结点,在此假定结点的ID范围为01~N,如超出此范围则代码不适用

    代码:

    编译器:C++(g++)

    #include <iostream>
    #include <vector>
    #include <stack>
    using namespace std;
    
    struct Node{
        int id;
        Node *left;
        Node *right;
        Node(int n=0):id(n),left(NULL),right(NULL){}
    };
    int main()
    {
        int n,m;
        cin>>n>>m;
        if(0==n)
        {
            return 0;
        }
        vector<Node> allNode(n,0);
        for(int i=0;i!=n;++i)
        {
            allNode[i].id=i+1;
        }
        for(int i=0;i!=m;++i)
        {
            int pre,num,cur;
            cin>>pre>>num;
            for(int j=0;j!=num;++j)
            {
                cin>>cur;
                if(0==j)
                {
                    allNode[pre-1].left=&(allNode[cur-1]);
                }
                else
                {
                    allNode[pre-1].right=&(allNode[cur-1]);
                }
                pre=cur;
            }
        }
        stack<Node> s1,s2;
        s1.push(allNode[0]);
        while(!s1.empty())
        {
            if(s1.top().id!=allNode[0].id)
            {
                cout<<" ";
            }
            int count=0;
            Node t;
            while(!s1.empty())
            {
                t=s1.top();
                s1.pop();
                if(NULL==t.left)
                {
                    ++count;
                }
                else
                {
                    s2.push(*(t.left));
                }
                if(t.right)
                {
                    s1.push(*(t.right));
                }
            }
            swap(s1,s2);
            cout<<count;
        }
        cout<<endl;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:甲级-1004 Counting Leaves (30 分)

          本文链接:https://www.haomeiwen.com/subject/ptvlyctx.html