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行对应编号第 ...
随机推荐
- APP前置代码
APP自动化前置代码: #导入包from appium import webdriverimport timedesired_caps = {}desired_caps['platformName'] ...
- spring boot jar的支持
- AcWing 314. 低买 (线性DP)打卡
题目:https://www.acwing.com/problem/content/316/ 题意:求一个最长单调递减子序列,然后并且求方案数,如果序列完全一样就不要了 思路:我们肯定时修改LIS,我 ...
- Map-Amap:目录
ylbtech-Map-Amap:目录 1. 高德地图返回顶部 1. https://www.amap.com/ 2. 2. 高德地图App返回顶部 1. http://www.autonavi.c ...
- MySQL数据库参数优化
概述 最近在对各个系统的mysql做一些参数上的优化,也开了慢查询,准备后面针对特定sql再进一步优化.下面主要介绍一下一些优化的参数. 1.优化前mysql配置 可以看到基本上是没怎么做优化的. 2 ...
- 封装token
let baseApiUrl = 'xxxx' var app = getApp(); let defaultHeaders = { 'Content-Type': 'application/json ...
- 目前写出的bug
要检测p->next 与p都不=NULL while(p->next!=NULL &&p!=NULL) 会导致访问access NULL pointer的runtime错误 ...
- day 109结算中心.
from django.db import models from django.contrib.contenttypes.fields import GenericForeignKey,Generi ...
- Linear Regression and Gradient Descent (English version)
1.Problem and Loss Function Linear Regression is a Supervised Learning Algorithm with input matrix ...
- VB - 变量
Cbool函数将变量转换成布尔值: Cbyte函数将变量转换为0到255之间的整数. Ccur函数.Cdbl函数和Csng函数将变量转换为浮点数值,前者只精确到小数点后四位,后两者要更加精确,数值的范 ...