PTA 03-树2 List Leaves (25分)
题目地址 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分)的更多相关文章
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- 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 ...
- 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 ...
- PTA 05-树7 堆中的路径 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径 (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...
- 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 ...
- PTA 07-图4 哈利·波特的考试 (25分)
哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变化的魔咒就是简单地将原来的魔咒倒过来念 ...
- L2-006 树的遍历 (25 分)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 题目: 给定一棵二叉树的后序遍历和中序 ...
- 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 ...
随机推荐
- django 相关问题
和数据库的连接 session的实现 django app开发步骤 python环境准备 数据库安装 model定义 url mapping定义 view定义 template定义 如何查看数据库里的 ...
- HDU 1221 Rectangle and Circle 考虑很多情况,good题
http://acm.hdu.edu.cn/showproblem.php?pid=1221 114 92 31 95 13 96 3 这题只需要判断圆和矩形是否相交,然后在里面是不算相交的. 那么就 ...
- php,json数据传输(无刷新)
废话不说直接上关键代码: js代码: <script language="javascript"> $(".login").live('click' ...
- archsummit_bj2016
http://bj2016.archsummit.com/schedule 大会日程 2016年12月02日,星期五 7:45-9:00 签到 8:45-9:00 开始入场 9:00-9:30 开场致 ...
- 小程序java解密
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16< ...
- AJPFX分享eclipse自动生成java注释方法
设置方法介绍:eclipse中:Windows->Preferences->Java->Code Style->Code Template->Comments,然后对应的 ...
- This is such a crock of shit—From Scent of a woman
- Mr. Slade. - This is such a crock of shit! - Mr. Trask. - Please watch your language, Mr. Slade. Y ...
- 《Python基础教程》 读书笔记 第五章(下)循环语句
5.5.1while循环 x=1 while x<=100: print x x+=1 确保用户输入了名字: name="" while not name: name=raw ...
- std::map插入已存在的key时,key对应的内容不会被更新
std::map插入已存在的key时,key对应的内容不会被更新,如果不知道这一点,可能会造成运行结果与预期的不一致 “Because element keys in a map are unique ...
- (转)Spring的三种实例化Bean的方式
http://blog.csdn.net/yerenyuan_pku/article/details/52832793 Spring提供了三种实例化Bean的方式. 使用类构造器实例化. <be ...