题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666

5-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 NN (\le 10≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1N−1. Then NNlines 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

大部分代码都是在做数据结构,其它的,就是简单的层序遍历
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 15:59 答案正确 25 5-4 gcc 2 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 13/13 1 1
测试点2 答案正确 5/5 2 1
测试点3 答案正确 1/1 1 1
测试点4 答案正确 5/5 2 1
测试点5 答案正确 1/1 1 1
*/
#include<stdio.h>
#include<stdlib.h>
#define MAXLEN 10
struct treenode{
int lc;
int rc;
int isLeaf;
int father;
}; typedef struct tree{
struct treenode nodes[MAXLEN];
int root;
int length;
}*ptrTree; typedef struct queue{
int data[MAXLEN];
int front;
int rear;
}*Que; Que CreateQueue()
{
Que temp;
temp=(Que)malloc(sizeof(struct queue));
temp->front=0;
temp->rear=0;
return temp;
} void EnQueue(Que Q,int item)
{
if((Q->rear+1)%MAXLEN == Q->front)
printf("Queue is full!");
Q->rear=(Q->rear+1)%MAXLEN;
Q->data[Q->rear]=item;
} int DeQueue(Que Q)
{
if(Q->front==Q->rear){
printf("Queue is Empty!");
return -1;
}
Q->front=(Q->front+1)%MAXLEN;
return Q->data[Q->front];
} int IsQueueEmpty(Que Q)
{
return Q->front==Q->rear;
} ptrTree CreateTree()
{
int i;
ptrTree temp;
temp=(ptrTree)malloc(sizeof(struct tree));
for(i=0;i<MAXLEN;i++)
temp->nodes[i].father=-1;
return temp; } void DestroyQueue(Que Q)
{
free(Q);
} void DestroyTree(ptrTree T)
{
free(T);
} void Input(ptrTree T)
{
int i,k,len;
scanf("%d",&len);
getchar();//skip a \n
T->length=len;
for(i=0;i<len;i++)
{
T->nodes[i].lc=getchar()-'0';
getchar();//skip a space
T->nodes[i].rc=getchar()-'0';
getchar();//skip a \n if(T->nodes[i].lc>=0)
T->nodes[T->nodes[i].lc].father=i;
if(T->nodes[i].rc>=0)
T->nodes[T->nodes[i].rc].father=i; if(T->nodes[i].lc<0 && T->nodes[i].rc<0)
T->nodes[i].isLeaf=1;
} for(i=0;i<len;i++){
if(T->nodes[i].father<0){
T->root=i;
break;
}
}
} void Process(ptrTree T,Que Q)
{
EnQueue(Q,T->root);
while(!IsQueueEmpty(Q))
{
int i;
i=DeQueue(Q);
if(T->nodes[i].isLeaf==1)
printf("%d",i);
else {
if(T->nodes[i].lc>=0)
EnQueue(Q,T->nodes[i].lc);
if(T->nodes[i].rc>=0)
EnQueue(Q,T->nodes[i].rc);
}
if(T->nodes[i].isLeaf==1 && !IsQueueEmpty(Q))
printf(" ");
}
} int main()
{
Que Q=CreateQueue();
ptrTree T=CreateTree();
Input(T);
Process(T,Q);
return 0;
}

  

PTA 03-树2 List Leaves (25分)的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  3. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

  4. PTA 10-排序5 PAT Judge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PA ...

  5. PTA 05-树7 堆中的路径 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径   (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...

  6. 7-4 List Leaves (25分) JAVA

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  7. PTA 07-图4 哈利·波特的考试 (25分)

    哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变化的魔咒就是简单地将原来的魔咒倒过来念 ...

  8. L2-006 树的遍历 (25 分)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...

  9. 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. I ...

随机推荐

  1. c库函数-字符串

    一 strok:从字符串中按照分隔符提取所有字串 char s[] = "水发产品,47.6,不合格,mg/kg,17-05-21 15:04;";  char *delim = ...

  2. Java 关键字volatile的解释

    volatile 关键字特征: 1.可见性,是指线程之间的可见性,一个线程修改的状态对另一个线程是可见的.可以禁止线程的工作内存对volatile修饰的变量进行缓存,并将修改的变量立即写入主存. 2. ...

  3. .NET Core WebAPI Swagger使用

    相对于普通的webapi而言,.net core webapi本身并不具备文档的功能,所以可以借助第三方插件:swagger,使用的话很简单. 步骤一. Nuget Packages安装,使用程序包管 ...

  4. Redis java操作客服端——jedis

    1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...

  5. AJPFX总结String类的特点

          String str = "abc"; str就是String的一个对象         字符串一旦被赋值, 值就不能再被改变了         举例:String s ...

  6. JUnit的好搭档-Hamcrest

    一.Hamcrest简介 Hamcrest是一个用于编写匹配器(matcher)对象的框架,允许以声明方式定义“匹配(match)”规则.它可以与JUnit框架配合使用,使断言可读更高,更加灵活(例如 ...

  7. markdown快捷键(转)

    markdown快捷键 GLYPH NAME COMMAND Command, Cmd, Clover, (formerly) Apple ALT Option, Opt, (Windows) Alt ...

  8. Vue2.0实现路由

    Vue2.0和1.0实现路由的方法有差别,现在我用Vue 2.0实现路由跳转,话不多说,直接上代码 HTML代码 <div class="tab"> <route ...

  9. 消息中间件与RPC的区别

    消息中间件和消息通信与RPC各自具有怎样的优势,如何互补消息中间件主要实现的是异步.弹性消息以及队列,弹性消息有时可以借助于外存从而一定程度上可以实现峰值缓存,有效均衡服务器端压力,同时消息可以进行一 ...

  10. EOS Dapp体验报告

    EOS Dapp体验报告 EOS通过并行链和DPOS的方式解决了延迟和数据吞吐量的难题. EOS能够实现每秒百万级的处理量,而目前比特币是每秒7笔,以太坊是30-40笔,EOS的这一超强能力吊打比特币 ...