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 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

My Code

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MaxTree 10
#define Tree int
#define ElementType int
#define Null -1
struct TreeNode{
    ElementType Data;
    Tree Left;
    Tree Right;
}T[MaxTree]; 
typedef int Position;
typedef struct QNode *PrtToQNode;
struct QNode{
    ElementType *Data;
    Position Front,Rear;
    int MaxSize;
};
typedef PrtToQNode Queue;
Queue CreateQueue(int MaxSize){
    Queue Q=(Queue)malloc(sizeof(struct QNode));
    Q->Data=(ElementType*)malloc(MaxSize * sizeof(ElementType));
    Q->Front=Q->Rear=0;
    Q->MaxSize = MaxSize;
    return Q;
}
bool IsFull(Queue Q){
    return ((Q->Rear+1)%Q->MaxSize==Q->Front);
}
void AddQueue(Queue Q,ElementType x){
    if(!IsFull(Q)){
        Q->Rear=(Q->Rear+1)%Q->MaxSize;
        Q->Data[Q->Rear]=x;
    }
}
bool IsEmpty(Queue Q){
    return (Q->Front==Q->Rear);
}
ElementType DeleteQueue(Queue Q){
    if(!IsEmpty(Q)){
        Q->Front=(Q->Front+1)%(Q->MaxSize);
        return Q->Data[Q->Front];
    }
    else return -1;
}
Tree BuildTree(struct TreeNode T[]){
    int N,i,Root=-1,check[MaxTree];
    char cl,cr;
    scanf("%d\n",&N);
    if(N){
        for(i=0;i<N;i++) check[i]=0;
        for(i=0;i<N;i++){
            scanf("%c %c\n",&cl,&cr);
            if(cl!='-'){
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            }
            else T[i].Left = Null;
            if(cr!='-'){
                T[i].Right = cr - '0';
                check[T[i].Right] = 1;
            }
            else T[i].Right = Null;
            T[i].Data = i;
        }
        for(i=0;i<N;i++)
            if(!check[i]) break;
        Root = i;
    }
    return Root;
}
void PrintLeaves(Tree R){
    Queue Q;
    int count = 0;
    int T1;
    if(R!=Null){
        Q = CreateQueue(MaxTree);
        AddQueue(Q,R);
        while(!IsEmpty(Q)){
            T1 = DeleteQueue(Q);
            if(T[T1].Left!=Null) AddQueue(Q,T[T1].Left);
            if(T[T1].Right!=Null) AddQueue(Q,T[T1].Right);
            if((T[T1].Left==Null)&&(T[T1].Right==Null)) {
                printf("%d",T1);
                count = 1;
            }
            if(count==1&&(!IsEmpty(Q))){
                printf(" ");
                count = 0;
            }
        }
    }
}
int main()
{
    Tree R;
    R = BuildTree(T);
    PrintLeaves(R);
    return 0;
}

Outcome:

Outcome
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容