MOOC

作者: 有苦向瓜诉说 | 来源:发表于2017-03-20 23:23 被阅读0次

03-树2 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 NNN (≤10\le 10≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1N-1N−1. Then NNN 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>
#define MaxSize 10
#define Tree int
#define Null -1

struct TreeNode{
    int Left,Right;
}T[MaxSize];

//create tree ,return root
Tree BuildTree(T[])
{
    int n;
    scanf("%d\n",&n);
    int check[MaxSize];
    for(int i=0;i<MaxSize;i++) check[i]=0;
    for(int i=0;i<n;i++){
        char a,b;
        scanf("%c %c\n",&a,&b);
        if(a!='-'){
            T[i].Left=a-'0';
            check[T[i].Left]=1;
        }
        else{
            T[i].Left=Null;
        }
        if(b!='-'){
            T[i].Right=b-'0';
            check[T[i].Right]=1;
        }
        else{
            T[i].Right=Null;
        }
    }
    int i=-1;
    for(i=0;i<n;i++){
        if(!check[i]) break;
    }
    return i;
}

void LevelLeaf(Tree root)
{
    int flag=0;
    int queue[MaxSize];
    int front=0,rear=0;//front point temp,rear point next;
    queue[rear++]=root;
    while(front<rear){
        Tree temp=queue[front++];
        if(T[temp].Left==Null && T[temp].Right == Null){
            if(flag==0) flag=1;
            else printf(" ");
            printf("%d",temp);
        }
        if(T[temp].Left!=Null) queue[rear++]=T[temp].Left;
        if(T[temp].Right!=Null) queue[rear++]=T[temp].Right;
    }
}

int main()
{
    freopen("in.txt","r",stdin);
    Tree root=BuildTree(T);
    LevelLeaf(root);
}

相关文章

  • 好好学习,怎能错过这些网站和APP

    公开课 MOOC(网站)http://mooc.guokr.com/ 中国大学MOOC (网站,APP) 学堂在线...

  • 在线教育五大商业模式

    一、MOOC模式 我们大家熟知的“慕课”就是MOOC的音译,而MOOC(massive open online c...

  • MOOC

    massive,open,online,course四个词的缩写,是大规模网络开放课程的简称。 十年后MOOC对未...

  • mooc

    拍mooc或者视频课程要特写镜头,这样看视频的人会觉得是老师一对一授课

  • MOOC

    03-树2 List Leaves (25分) Given a tree, you are supposed ...

  • 10个令你变强的网站

    1.MOOC学院 MOOC学院是果壳网旗下的一个讨论MOOC课程的学习社区,收录Coursera,Udacity,...

  • 学习网站推荐

    1、MOOC学院 MOOC学院是果壳网旗下的一个讨论MOOC 课程的学习社区,收录Coursera,Udacity...

  • 打开MOOC的正确方式(3)

    续接上篇打开MOOC的正确方式(1),打开MOOC的正确方式(2)。 说完MOOC的定义、目标,以及各平台优缺点之...

  • 打开MOOC的正确方式(4)

    续接上篇打开MOOC的正确方式(1)打开MOOC的正确方式(2)打开MOOC的正确方式(3) 忽然发现还有几天就开...

  • 坚持写作100天,为自己加油!

    我关注MOOC时间不长。还是在2014年5月31日央视的新闻调查《慕课来了》,我开始认识MOOC。[MOOC...

网友评论

      本文标题:MOOC

      本文链接:https://www.haomeiwen.com/subject/bxcwnttx.html