PTA--03-树2 List Leaves

这种题目没有用我们常规的离散型的结点去构造树,而是采用结构体数组形式表达。

题目

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 NN (\le 10≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N-1N−1. Then NN 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

解释

也就是说,首先输入一个数字表示结点数N,结点编号从0-N-1,下面的N行顺序输入0-N-1号结点的左右孩子的编号。如果没有的话,就用’-‘表示。

思路

只需要理清楚用什么样的结构体存储,如何构建树,再考虑如何遍历。

因为采用的是结构体数组存储,只要把题中的关系输入进去,逻辑上一棵树就建好了。

题目要求的输出是层序遍历,因此假设结点之间的树关系已经构造好,那么借助一个队列来实现层序遍历,在输出的时候加以判断是不是叶子结点即可。

代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef struct Node
{
    int index;
    int left;
    int right; 
} Node;

int generateTree(vector<Node> &nodes,int n) //返回根节点的index
{
    vector<int> checked(n,0);
    int root;
    if(n < 1)
    {
        root = -1;
    }
    for(int i = 0; i < n; i++)
    {
        Node node;
        node.index = i;
        char cl,cr;
        cin >> cl >> cr;

        if(cl != '-') // 有左孩子,那么左边结点就不会是根
        {
            node.left = cl - '0';
            checked[node.left] = 1;
        }
        else
        {
            node.left = -1;
        }
        if(cr != '-') // 有右孩子
        {
            node.right = cr - '0';
            checked[node.right]  = 1; // 右孩子不会是根
        }
        else
        {
            node.right = -1;
        }
        nodes.push_back(node);
    }
    for(int i = 0; i < n; i++) // 遍历,找到根结点
    {
        if(checked[i] == 0)
        {
            root = i;
        }
    }
    return root;
}

void levelTraversePrint(vector<Node> &nodes, int root)
{
    if(root == -1)
    {
        return;
    }
    queue<Node> que;
    que.push(nodes[root]);
    int count  = 0; // 用于辅助判断是不是最后一个结点
    while(que.size())
    {
        Node node = que.front();
        count++;
        // 加一个是不是叶子的判断
        if(node.left == -1 && node.right == -1)
        {
            if(count == nodes.size())
            {
                cout << node.index;
            }
            else
            {
                cout << node.index << ' ';
            }
        }

        que.pop(); // 弹出队首

        if(node.left != -1)
        {
            que.push(nodes[node.left]);
        }
        if(node.right != -1)
        {
            que.push(nodes[node.right]);
        }
    }
}

int main()
{
    int n;
    cin >> n;
    vector<Node> nodes;
    int root =  generateTree(nodes,n);

    levelTraversePrint(nodes,root);

    cout << endl;
}

以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值