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 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
我的答案
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #define MaxTree 10
#define Tree int
#define Null -1 struct TreeNode {
Tree Left;
Tree Right;
} T1[MaxTree]; #define QueueSize 100
struct QNode {
int Data[QueueSize];
int rear;
int front;
};
typedef struct QNode *Queue; int IsEmpty(Queue PtrQ)
{
return (PtrQ->front == PtrQ->rear);
} void AddQ(Queue PtrQ, int item)
{
if((PtrQ->rear+)%QueueSize == PtrQ->front) {
printf("Queue full");
return;
}
PtrQ->rear = (PtrQ->rear+)%QueueSize;
PtrQ->Data[PtrQ->rear] = item;
} int DeleteQ(Queue PtrQ)
{
if(PtrQ->front == PtrQ->rear) {
printf("Queue empty");
return -;
} else {
PtrQ->front = (PtrQ->front+)%QueueSize;
return PtrQ->Data[PtrQ->front];
}
} Tree BuildTree(struct TreeNode T[])
{
char cl, cr;
int N, i, check[MaxTree];
Tree Root = Null;
// scanf("%d\n", &N);
scanf("%d\n", &N);
// printf("N=%d\n", N);
if(N) {
for(i=;i<N;i++)
check[i] = ;
for(i=;i<N;i++) {
scanf("%c %c\n", &cl, &cr);
// printf("cl=%c, cr=%c\n", cl, cr);
if(cl!='-') {
T1[i].Left = cl - '';
check[T1[i].Left] = ;
} else
T1[i].Left = Null; if(cr!='-') {
T1[i].Right = cr - '';
check[T1[i].Right] = ;
} else
T1[i].Right = Null;
}
for(i=;i<N;i++)
if(check[i] != ) break;
Root = i;
}
return Root;
} void PrintLeaves(Tree R) //层序遍历
{
Queue Q;
Tree cur;
int count = ;
if(R==Null) return;
Q = (Queue)malloc(sizeof(struct QNode));
Q->rear = -;
Q->front = -;
AddQ(Q, R);
while(!IsEmpty(Q)) {
cur = DeleteQ(Q);
if((T1[cur].Left == Null) && (T1[cur].Right == Null)) {
if(!count) {
printf("%d", cur);
count++;
} else
printf(" %d", cur);
continue;
}
if(T1[cur].Left!=Null) AddQ(Q, T1[cur].Left);
if(T1[cur].Right!=Null) AddQ(Q, T1[cur].Right);
}
} int main()
{
Tree R;
R = BuildTree(T1); PrintLeaves(R); return ;
}
03-树2 List Leaves(25 分)的更多相关文章
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- 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 ...
- 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 ...
- 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 ...
- 浙大数据结构课后习题 练习三 7-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. I ...
- 7-3 树的同构(25 分) JAVA
给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...
- PTA 03-树1 树的同构 (25分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构 (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ...
- PTA 树的同构 (25分)
PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...
随机推荐
- 报错——userdel: user hhh is currently used by process 9218
报错 userdel: user hhh is currently used by process 9218 [root@centos71 ~]# useradd hhh [root@centos71 ...
- spring-boot整合mongodb的案例
1.简介 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品 ...
- php rtrim()函数 语法
php rtrim()函数 语法 rtrim()函数怎么用? php rtrim()函数用于删除字符串右边的空格或其他预定义字符,语法是rtrim(string,charlist),返回经过charl ...
- 4412 移植mpu9250尝试
4412的板子IO都是1.8v的.只有I2C6是用了电平转换到了3.3v.所以我准备使用I2C6来驱动mpu9250 一.首先去掉占用的模块 menuconfig中去掉触摸的驱动 Device Dri ...
- 【Elasticsearch】Elasticsearch索引的创建、查看及修改
转: 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/liuxiao723846/art ...
- IGServer for Java
Eclipse和JavaEE: DCServer是哪个? 查看服务器文件夹: Env_Var变量没有定义:JRE_HOME.JDK_HOME 这是Tomcat报错的提示,但是既然JAVA_HOME都有 ...
- flutter中的列表组件
列表布局是我们项目开发中最常用的一种布局方式.Flutter 中我们可以通过 ListView 来定义列表项,支持垂直和水平方向展示.通过一个属性就可以控制列表的显示方向.列表有以下分类: 垂直列表 ...
- get the deadlock information from sql server
https://stackoverflow.com/questions/12422986/sql-query-to-get-the-deadlocks-in-sql-server-2008 You c ...
- 实现自己的shell--MIT xv6 shell
参考代码: #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcnt ...
- python rpy2,tkinter安装问题解决
windows系统下 在python中直接pip install rpy2时,会出错,没仔细看错误,直接下载了whl文件(https://www.lfd.uci.edu/~gohlke/pythonl ...