• PTA 5-4 List Leaves (25)


    题目:http://pta.patest.cn/pta/test/16/exam/4/question/666 

    PTA - Data Structures and Algorithms (English) - 5-4

    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      //a positive integer N (≤10) which is the total number of nodes in the tree
    1 -    //then each line corresponds to a node (0 to N-1)
    - -    //and gives the indices of the left and right children of the node 
    0 -    //If the child does not exist, a "-" will be put at the position
    2 7
    - -
    - -
    5 -
    4 6
    Sample Output:
    4 1 5  //print all the leaves' indices in the order of top down, and left to right
           //no extra space at the end of the line

    代码:

    法一:用STL中的适配器 queue (直接queue<Type> x:默认基于deque实现)
    法二:自己建queue
    #include <iostream>
    //#include <fstream>
    #include <queue>
    using namespace std;
    const int maxsize = 10;
    
    typedef struct
    {
        char left;   //设类型为char:因为含有 '-'
        char right;
    }ElemType;
    
    typedef struct node
    {
        int data;
        int ileft, iright;
        struct node *left, *right;
    }TreeNode;
    TreeNode tree[maxsize];
    
    queue<int> que;
    
    void leveltravel(int root)
    {
        int del; //记录出队的结点
        int zuihou[maxsize], k=0; //用来控制输出格式:结尾没有空格
        que.push(root);
        while(!que.empty())     //队列不为空
        {
            del=que.front();
            que.pop();
            //!出队的若为叶子结点,先存到zuihou[]中
            if(tree[del].left==NULL && tree[del].right==NULL)
            {
                zuihou[k]=tree[del].data;
                k++;
            }
            if(tree[del].left)
                que.push(tree[del].ileft);
            if(tree[del].right)
                que.push(tree[del].iright);
        }
        //!输出
        cout << zuihou[0];
        for(int i=1; i<k; i++)
            cout << ' ' << zuihou[i];
    }
    int main()
    {
        /*文件输入
        ifstream fin("test.txt"); //创建对象,并通过构造函数打开文件
        int n;
        fin >> n;
        ElemType a[n];
        for(int i=0; i<n; i++)
            fin >> a[i].left >> a[i].right;
        fin.close();
        */
        //!终端输入
        int n;
        cin >> n;
        //!输入:ElemType a[n] 存放输入的左右孩子信息
        ElemType a[n];
        for(int i=0; i<n; i++)
            cin >> a[i].left >> a[i].right;
    
        //!建树:TreeNode tree[n] 存放树的信息
        for(int i=0; i<n; i++)
        {
            tree[i].data=i;
            if(a[i].left != '-')
            {
                tree[i].ileft = a[i].left-'0';
                tree[i].left = &tree[a[i].left-'0'];   //找到左子树并连接上
            }
            else
                tree[i].left = NULL;
            if(a[i].right != '-')
            {
                tree[i].iright = a[i].right-'0';
                tree[i].right = &tree[a[i].right-'0']; //找到右子树并连接上
            }
            else
                tree[i].right = NULL;
        }
        //!找root: int flat[n] 不是则标记为1
        int root;
        int flat[n];
        for(int i=0; i<n; i++)
        {
            if(a[i].left!='-')
                flat[tree[i].ileft]=1;
            if(a[i].right!='-')
                flat[tree[i].iright]=1;
        }
        for(int i=0; i<n; i++)
        {
            if(flat[i]!=1)
            {
                root=tree[i].data;
                break;
            }
        }
        //!按层次遍历
        leveltravel(root);
        return 0;
    }
     
    法二:自己建queue
    #include <iostream>
    using namespace std;
    #define maxsize 10
    
    typedef struct
    {
        char left;
        char right;
    }elemtype;
    
    typedef struct node
    {
        int data;
        int ileft, iright;
        struct node *left, *right;
    }treenode;
    
    typedef struct
    {
        int data[maxsize];
        int front;
        int rear;
    }queue;
    
    treenode tree[maxsize];
    queue q;
    
    //在队尾插入结点
    void addq(int t)
    {
        q.data[q.rear]=tree[t].data;
        q.rear++;
    }
    //在队首取出并删除结点
    int deleteq()
    {
        int ret=q.data[q.front];
        q.front++;
        return ret;
    }
    //层序遍历
    void leveltravel(int root)
    {
        int del;
        int zuihou[maxsize],k=0; //!用来控制输出格式:结尾没有空格
        //!queue q;
        q.front=q.rear=0;
    
        addq(root);
        while(q.front!=q.rear)     //!队列不为空
        {
            del=deleteq();
    
            //!出队的若为叶子结点,先存到zuihou[]中
            if(tree[del].left==NULL && tree[del].right==NULL)
            {
                zuihou[k]=tree[del].data;
                k++;
            }
            if(tree[del].left)
                addq(tree[del].ileft);
            if(tree[del].right)
                addq(tree[del].iright);
        }
        //!格式输出
        cout << zuihou[0];
        for(int i=1;i<k;i++)
            cout << ' ' << zuihou[i];
    }
    int main()
    {
        int n;
        cin >> n;
        //!输入:ElemType a[n] 存放输入的左右孩子信息
        elemtype a[n];
        for(int i=0;i<n;i++)
            cin >> a[i].left >> a[i].right;
        //!建树:TreeNode tree[n] 存放树的信息
        for(int i=0; i<n; i++)
        {
            tree[i].data=i;
            if(a[i].left != '-')
            {
                tree[i].ileft = a[i].left-'0';
                tree[i].left = &tree[a[i].left-'0'];
            }
            else
                tree[i].left = NULL;
            if(a[i].right != '-')
            {
                tree[i].iright = a[i].right-'0';
                tree[i].right = &tree[a[i].right-'0'];
            }
            else
                tree[i].right = NULL;
        }
        //!找root: 不是则标记为1
        int root;
        int flat[n];
        for(int i=0;i<n;i++)
        {
            if(a[i].left!='-')
                flat[tree[i].ileft]=1;
            if(a[i].right!='-')
                flat[tree[i].iright]=1;
        }
        for(int i=0;i<n;i++)
        {
            if(flat[i]!=1)
            {
                root=tree[i].data;
                break;
            }
        }
        //!按层次遍历
        leveltravel(root);
        return 0;
    }
    
  • 相关阅读:
    [色彩校正] Gamma Correction
    需要齐次坐标的原因之二 所有的变换运算(平移、旋转、缩放)都可以用矩阵乘法来搞定
    需要齐次坐标的原因之一
    数据库连接类
    简单的数组排序
    OfficePage封装代码
    新闻管理数据模板
    最新page页码生成控件代码
    新闻管理cs页面
    快速收录新域名网站
  • 原文地址:https://www.cnblogs.com/claremore/p/4806208.html
Copyright © 2020-2023  润新知