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. Dataphin数据服务系列之--API 配置、管理和消费

    研发小哥哥还在为公司里大量 API 只上不下,不可查不可用, 想找的 API 找不到而苦恼吗?业务方小姐姐还在为 API 开发时间长,业务相应不及时而抱怨吐槽吗? 铛铛铛,Dataphin 数据服务 ...

  2. powerdesigner 15.1 逆向工程 sqlserver2008 、sqlserver2005 带注释

    第一种方法:在第一个网址里面的代码可以直接赋值到对应位置即可 http://wjqe.blog.163.com/blog/static/19938452011612536439/ 第二种方法:可塑性较 ...

  3. 【HDOJ6598】Harmonious Army(最小割)

    题意:有n个人,每个人可以从A,B两种职业中选择一种 有m对两人组,如果两个人都是A能获得p的收益,一个A一个B能获得q的收益,都是B能获得r的收益,其中q=p/4+r/3,保证p%4=0,r%3=0 ...

  4. 牛客假日团队赛9 A 乘积最大 (简单DP)

    题目:https://ac.nowcoder.com/acm/contest/1071/A 题意:给你一个串,然后给你m个乘号,用m个乘号分割开这个串,然后求分割可以求出的最大值 思路:首先范围很小 ...

  5. HashMap的底层原理 cr:csdn:zhangshixi

    1.    HashMap概述: HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变 ...

  6. [CSP-S模拟测试]:Lost My Music(凸包)

    题目描述 小$w$在天堂看到了一棵世界树.世界树上有$n$个节点,其中$1$节点为根,每个节点有一个正整数权值$c_i$.现在小$w$想要对每个节点$u$求出它的祖先$v$中$\frac{c_v-c_ ...

  7. Gradle Wrapper 介绍

    回顾 上一篇介绍了如何使用 Gradle 快速创建 Java 项目,开始讲 Gradle Wrapper 之前先来重温一下通过 gradle init 命令创建的 Java 项目目录结构 ├── bu ...

  8. CET-6 分频周计划生词筛选(Week 2)

    点我阅读 Week 2 2016.09.04/05 p58 ongoing / forward p59 prosperity p60 rear p61 rival + segregation + se ...

  9. cabal替代脚本

    由于网络原因,直接使用cabal update不成功,只能自己写脚本直接从网上拖包下来,自己安装. 但是这样做的缺点是需要手动处理dependency,当然,也可以把脚本写的复杂些,自动来处理depe ...

  10. Gentoo 搭遗

    安装 app-portage/gentoolkit 使用如下方式,可查询某个被包安装的文件列表,可以筛选文件类型 equey files [--filter=doc] <包名称> emer ...