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行对应编号第 ...
随机推荐
- [CSP-S模拟测试]:Endless Fantasy(DFS)
题目描述 中二少年$cenbo$幻想自己统治着$Euphoric\ Field$.由此他开始了$Endless\ Fantasy$.$Euphoric\ Field$有$n$座城市,$m$个民族.这些 ...
- Android实战技巧:Dialog (转)
转:http://blog.csdn.net/hitlion2008/article/details/7567549#t0 Dialog是任何系统都必须有的一个控件,作为辅助窗口,用于显示一些消息,或 ...
- 2016年Esri技术公开课全年资料分享
大家好,2016年的公开课活动在上周全部结束,感谢大家的支持. 2016年的公开课共进行20期,共有24位讲师参与,公开课视频播放.课件下载次数累计超10万次,在这里衷心的感谢大家的积极参与和分享精神 ...
- STM32几个IO的工作模式
浮空,顾名思义就是浮在空中,上面用绳子一拉就上去了,下面用绳子一拉就沉下去了. 开漏,就等于输出口接了个NPN三极管,并且只接了e,b. c极 是开路的,你可以接一个电阻到3.3V,也可以接一个电阻 ...
- 公司-IT-SanSan:SanSan
ylbtech-公司-IT-SanSan:SanSan 毫不费力的组织.无缝简单.基于名片的联系人管理 SanSan是一个名片管理应用,为企业提供内部联系人管理和分享服务,此外该公司也是日本最大的.基 ...
- 《图解设计模式》读书笔记6-2 Chain of Responsibility模式
目录 1. 简介 2. 示例程序 类图 代码 3. 模式的角色和类图 角色 类图 4. 思路拓展 1. 简介 Chain of Responsibility模式是责任链模式,模式的核心就是转移责任.就 ...
- Java 时间相关
java的时间主要关注这几个类,查看Java API 1.6 java.util.Calendar Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MON ...
- Pipenv管理项目环境--Django项目的一些最佳实践
virtualenv --- 使用不方便 提升效率,管理更便捷--- pipenv 新建环境:: pip3 install pipenv 在项目下,用pipenv安装 Djagno pipenv in ...
- Python3学习(18)--偏函数(Partial) --转载存地址
由于最近出差,没有时间更, 倒不是忙,而是费心,项目其实并不难,主要是涉及钱的地方谈技术略显苍白:没有技术解决不了的问题,但是钱没到位,没人愿意无偿给你提供技术,算是停更一周后的,吐槽吧. 赶上今天 ...
- CF560补题
D题:用来对比的vector<long long> b不能被初始化大小成n,因为a里面有n个因子,但是这是可能存在遗漏情况的.如果刚好是遇到实际因子远多于n,那么就会在运行过程中出错. 还 ...