[DS_PRACTICE]List Leaves

这道题是浙江大学mooc上的一道题,我初学数据结构,对树的建立与遍历还有很多不熟悉的地方。因此做这道题,我写代码花了3h,debug2h,但是总归是值得的。很多时候当我做完难题,突然觉得也不过如此。
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.
在这里插入图片描述
以下是个人的一些思考,这题是让我们从上到下,从左到右输出叶子。一开始我的想法大框架是这样的,先用结构数组建立一棵树,然后采用随便一种遍历方法,在遍历的过程中找到叶子,并保存高度,然后将叶子存入结构数组中,最后再进行输出。后来我发现,采用层序遍历的方法会自动按题目要求输出所要求的,这就是我采用层序遍历的原因。过程中还有很多低级错误,比如判断语句中只写了一个等号,还有写了两个左儿子(本应对左儿子右儿子进行分别判断)。代码可能稍微有点长。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

const int Null = -1;

typedef struct TreeNode{
    int id;
    int cl;
    int cr;
}Tree;

typedef struct Queue{
    int font;
    int rear;
    int capacity;
    Tree treeArray[10];
}queue;

int root;queue* Q;int num;Tree R[10];

void BuildTree();/* 建树并返回根节点 */
void TraversalTree();/* 将建好的树传入并且保存每一片叶子 */
void CreateQueue();
void Push(queue* Q,Tree element);
Tree Pop(queue* Q);/* 返回一个pop出来的节点 */

int main()
{
    int root;
    BuildTree();
    CreateQueue();
    TraversalTree();
}

void BuildTree()
{
    int i,check[10];/* 记录树的节点数 */
    scanf("%d",&num);getchar();
    /* 判断根节点的数组先全部初始化为0 */
    for(i = 0; i < 10;i++)
        check[i] = 0;
    /* 如果不为空数组就进行 */
    if(num){
        /* 进行每一行的输入 */
        for(i = 0;i < num;i++){
            char cl,cr;
             scanf("%c %c",&cl,&cr);
             getchar();
             R[i].id = i;
             if(cl != '-')/* 如果cl不为0 */
             {
                R[i].cl = cl - '0';
                check[R[i].cl] = 1;
             }
             else R[i].cl = Null;
             if(cr != '-')
             {
                 R[i].cr = cr -'0';
                 check[R[i].cr] = 1;
             }
             else R[i].cr = Null;
        }
        /* 遍历check数组,查找为0,该点为root */
        for(i = 0;i < num;i++)
            if(check[i] == 0) root = i;
    }
}

void TraversalTree()
{
    int i,j=0;
    int leaves[10];
    Push(Q,R[root]);
    if(R){
        for(i = 0;i < num;i++){
           Tree tmp = Pop(Q);
           if(tmp.cl == Null && tmp.cr == Null) leaves[j++]=tmp.id;
           if(tmp.cl != Null ) Push(Q,R[tmp.cl]);  
           if(tmp.cr != Null ) Push(Q,R[tmp.cr]); 
        }
    }
    for(i = 0;i < j-1;i++)
        printf("%d ",leaves[i]);
    printf("%d",leaves[j-1]);
}

void CreateQueue(){
    Q = (queue*)malloc(sizeof(queue));
    Q->font = 0;
    Q->rear = 0;
    Q->capacity = 10;
}

Tree Pop(queue* Q){
    Tree temp;
    temp = Q->treeArray[Q->font%Q->capacity];
    Q->font++;
    return temp; 
}

void Push(queue* Q,Tree element){
    Q->treeArray[Q->rear%Q->capacity] = element;
    Q->rear++;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值