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 分)的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

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

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

  5. 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 ...

  6. 浙大数据结构课后习题 练习三 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. 7-3 树的同构(25 分) JAVA

    给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的. 例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树 ...

  8. PTA 03-树1 树的同构 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/711 5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右 ...

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

  10. PTA 树的同构 (25分)

    PTA 树的同构 (25分) 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号):随后N行,第i行对应编号第 ...

随机推荐

  1. Android USB驱动源码分析(-)

    Android USB驱动中,上层应用协议里最重要的一个文件是android/kernel/drivers/usb/gadget/android.c.这个文件实现USB的上层应用协议. 首先包含了一些 ...

  2. 洛谷 P3806 (点分治)

    题目:https://www.luogu.org/problem/P3806 题意:一棵树,下面有q个询问,问是否有距离为k的点对 思路:牵扯到树上路径的题都是一般都是点分治,我们可以算出所有的路径长 ...

  3. 安卓Activity布局简述

    Activity布局简述 基于xml的布局 Main.xml(调用工程res/layout/main.xml定义的界面元素完成布局显示) <?xml version="1.0" ...

  4. flex布局滚动问题,子元素无法全部显示的解决办法

    flex布局使用起来非常方便,对于水平垂直居中的需求,很容易就能实现.但是前不久,在做全屏弹窗遮罩登录的时候,遇到了flex布局滚动的一个问题,在此记录一下. 问题重现 理想情况下,当然是下面的状态, ...

  5. laravel框架手动删除迁移文件后再次创建报错

    手动删除laravel框架数据表迁移文件后再次创建报错 如下图: 执行创建操作之后会在autoload_static.php及autoload_classmap.php这两个文件中添加迁移文件的目录. ...

  6. shell 截取变量的字符串

    假设有变量 var=http://www.linuxidc.com/test.htm一 # 号截取,删除左边字符,保留右边字符.echo ${var#*//}其中 var 是变量名,# 号是运算符,* ...

  7. 解决&#65279产生的空白行

    <script> var a=document.body.innerHTML; document.body.innerHTML=a.replace(/\ufeff/g,''); </ ...

  8. P3375 【模板】KMP字符串匹配——kmp算法

    先上一波题目 https://www.luogu.org/problem/P3375 kmp模板 看了好久才想起来是个什么东西qwq #include<cstdio> #include&l ...

  9. windows 虚拟内存查看

  10. scrapy错误-[scrapy.core.scraper] ERROR: Spider error processing

    一.问题,就是我的callback没得回调函数 二:然后我查看源代码,发现: 三.我把解析页数的函数名设置为,def parse(self,response):  就没保错了 能运行成功 总结:在sp ...