数据结构-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

运行结果

CaseHintResultRun TimeMemory
0sample 编号乱序,有单边左孩子,中间层少先输出Accepted3 ms352 KB
1最大N,有单边右孩子,多层Accepted3 ms296 KB
2最小NAccepted2 ms296 KB
3每层都有输出,有双孩子Accepted3 ms296 KB
4单边树,只有1个输出Accepted3 ms360 KB

程序


#include<iostream>
using namespace std;

#define Tree int
#define Null -1
#define ElementType int
#define MaxTree 10

//定义二叉树
struct BinTree{
    ElementType Element;
    Tree Left;
    Tree Right;
}BT[MaxTree];

//定义队列数据块
typedef struct Node *PtrToNode;
struct Node{
    ElementType Data;
    PtrToNode Next;
};
typedef PtrToNode List;

//定义链表队结构
typedef struct QNode *Queue;
struct QNode{
    PtrToNode Rear;//指向队尾的结点
    PtrToNode Front;//指向队头的结点
    int MaxSize;//队列长度
    int Size;//当前队列已占用空间
};

//Tree BuildBT_test(struct BinTree T[]);
Tree BuildBT(struct BinTree T[]);
Queue CreateQueue(int MaxSize);
void AddQ(Queue Q, ElementType Element);
int DeleteQ(Queue Q);
int IsEmptyQ(Queue Q);
int IsFullQ(Queue Q);
int* LevelTraversal(BinTree BT[], int boot);

int main()
{
    int boot;
    int *TreeLeaves;
    /*
    //测试用例
    boot = BuildBT_test(BT);
    */
    //构建二叉树
    boot = BuildBT(BT);
    //层次遍历,由上到下,由左到右输出叶子结点
    TreeLeaves = LevelTraversal(BT, boot);

    //叶结点个数
    int NLeaves = 0;
    for(int i=0; i<MaxTree; i++)
        if(TreeLeaves[i] >= 0) ++NLeaves;
    //按照输出要求输出叶结点
    if(NLeaves){
        for(int i=0; i<NLeaves-1; i++){
            cout << TreeLeaves[i] << " ";
        }
        cout << TreeLeaves[NLeaves-1] <<endl;
    }

    return 0;
}

int* LevelTraversal(BinTree BT[], int boot)
{
    int TreeElement;
    //存放叶结点的数组
    static int TreeLeaves[MaxTree];
    int TreeLeaves_i = 0;

    for(int i=0; i<MaxTree; i++) TreeLeaves[i] = -1;
    //非空树
    if(boot >= 0){
        //创建队长为MaxTree的队列
        Queue Q = CreateQueue(MaxTree);
        //将根结点的值传入队列中
        AddQ(Q, BT[boot].Element);
        //队列为空
        while(!IsEmptyQ(Q)){
            //出队
            TreeElement = DeleteQ(Q);
            if( (BT[TreeElement].Left == Null) && (BT[TreeElement].Right == Null)){
                TreeLeaves[TreeLeaves_i] = TreeElement;
                ++TreeLeaves_i;
            }
            if(BT[TreeElement].Left >=0) AddQ(Q, BT[TreeElement].Left);
            if(BT[TreeElement].Right >=0) AddQ(Q, BT[TreeElement].Right);
        }
    }
    return TreeLeaves;
}

int DeleteQ(Queue Q)
{
    int TreeElement;

    TreeElement = Q->Front->Data;
    //只有一个队列元素
    if( Q->Front == Q->Rear){
        Q->Front = NULL;
        Q->Rear = NULL;
    }
    else
        Q->Front = Q->Front->Next;
    --Q->Size;

    return TreeElement;
}

void AddQ(Queue Q, ElementType Element)
{
    //队列未满
    if(!IsFullQ(Q)){
        List node = new Node;
        node->Data = Element;
        node->Next = NULL;
        //队列为空,激活队列头结点
        if(IsEmptyQ(Q)){
            Q->Front = node;
        }
        else
            Q->Rear->Next = node;
        Q->Rear = node;

        ++Q->Size;
    }
}

int IsEmptyQ(Queue Q)
{
    if(Q->Size == 0)
        return 1;
    else
        return 0;
}

int IsFullQ(Queue Q)
{
    if(Q->Size >= Q->MaxSize)
        return 1;
    else
        return 0;
}

Queue CreateQueue(int MaxSize)
{
    Queue Q;
    Q = new QNode;
    Q->Front = NULL;
    Q->Rear = NULL;
    Q->MaxSize = MaxSize;
    Q->Size = 0;
    return Q;
}

Tree BuildBT(struct BinTree T[])
{
    int N = 0;
    char cl, cr;
    int boot = Null;

    cin >> N;

    //非空树
    if(N){
        int check[N];
        for(int i=0; i<N; i++){check[i] = 0;}
        for(int i=0; i<N; i++){
            T[i].Element = i;
            cin >> cl;
            cin >> 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;
        }
        int i;
        for(i=0; i<N; i++){
            if(!check[i])   break;
        }
        boot = i;
    }
    return boot;

}


Tree BuildBT_test(struct BinTree T[])
{
    int N = 8;
    char input[N][2] = \
    {   {'1', '-'},\
        {'-', '-'},\
        {'0', '-'},\
        {'2', '7'},\
        {'-', '-'},\
        {'-', '-'},\
        {'5', '-'},\
        {'4', '6'}\
    };
    char cl, cr;
    int check[N];
    int boot = Null;

    //非空树
    if(N){
        for(int i=0; i<N; i++){check[i] = 0;}
        for(int i=0; i<N; i++){
            T[i].Element = i;
            cl = input[i][0];
            cr = input[i][1];
            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;
        }
        int i;
        for(i=0; i<N; i++){
            if(!check[i])   break;
        }
        boot = i;
    }
    return boot;
}


 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值