PTA List Leaves(非链表题解)

题目如下:
7-4 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 (≤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
1 -

0 -
2 7

5 -
4 6
Sample Output:
4 1 5

思路:本题真正的难点不在于如何找到叶结点,而是如何按照题目的意思(in the order of top down, and left to right)从上到下,从左到右,将序号输出。
本题解使用队列方式存放所有结点的序号顺序,类似于层序遍历,由于该题并不复杂,因此运用了数组的方式存放,代码如下。

#include
#define maxsize 15//N (≤10)
struct tree{
int left;
int right;
}t[maxsize];
typedef struct tree Tree;
int num;
int buildtree(Tree t[])//建立树,存放左右儿子的位置,并返回根结点位置
{
    int root,i,check[maxsize]={0};//利用check数组检查哪个位置的结点从未是别的结点的儿子,即该结点为根结点
    scanf("%d\n",&num);
    if(num)
    {
        for(i=0;i<num;i++)
    {
        scanf("%c %c\n",&t[i].left,&t[i].right);
        if(t[i].left=='-')
            t[i].left=-1;
        else
            {
                t[i].left-='0';
                check[t[i].left]=1;
            }
        if(t[i].right=='-')
            t[i].right=-1;
        else
            {
                t[i].right-='0';
                check[t[i].right]=1;
            }
    }
    for(i=0;i<num&&check[i];i++);
    root=i;
    }
    else
        root=-1;
    return root;
}
int main()
{
    int r,cnt=0;//cnt用来控制输出的空格,第一个数据前不用输出空格,之后的每一个数据输出前都需要先输出空格
    r=buildtree(t);
    int queue[maxsize];
    for(int i=0;i<maxsize;i++)
        queue[i]=-1;
    queue[0]=r;//队列第一个为根结点位置
    for(int i=1;i<num;)
    {
        if(t[r].left!=-1&&t[r].right!=-1)
        //如果该结点有左右儿子,则将左儿子和右儿子一次存放在之后的队列中(先左后右)
        {
            for(int j=0;j<maxsize;j++)//该循环用来确定队列当前位置
                {
                    if(queue[j]==-1)
                {
                    queue[j]=t[r].left;
                    break;
                }
                }
            for(int j=0;j<maxsize;j++)
                {
                    if(queue[j]==-1)
                {
                    queue[j]=t[r].right;
                   break;
                }
                }
        }
        else if(t[r].left!=-1&&t[r].right==-1)//如果有左儿子,没有右儿子,将左儿子排到队列后面,同理右儿子
        {
            for(int j=0;j<maxsize;j++)
                {
                    if(queue[j]==-1)
                {
                    queue[j]=t[r].left;
                    break;
                }
                }
        }
        else if(t[r].right!=-1&&t[r].left==-1)
        {
            for(int j=0;j<maxsize;j++)
                {
                    if(queue[j]==-1)
                {
                    queue[j]=t[r].right;
                    break;
                }
                }
        }
        //无左儿子且无右儿子则不做调整
        r=queue[i++];//更新根的位子,看下一个结点是否有儿子,有则加入队列
    }
    for (int i=0;i<num;i++)//按队列顺序查看是否为叶结点,有则输出
    {
        if(t[queue[i]].left==-1&&t[queue[i]].right==-1)
        {
            if(cnt++)//第一个数据前不用输出空格,之后的每一个数据输出前都需要先输出空格
            printf(" ");
            printf("%d",queue[i]);
        }
    }
    return 0;
}

你可能感兴趣的:(PTA List Leaves(非链表题解))