PAT003 List Leaves
题目:
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
分析: 主要有两步,一是根据输入数据创建树,二是对树进行层序遍历。其中用到了通过链表创建的队列。
代码(c):
#include <stdio.h>
typedef struct listNode { int value;
char leftIndex, rightIndex;
struct listNode *left;
struct listNode *right;
struct listNode *next; // 队列用 } ListNode; typedef struct queue { ListNode *front;
ListNode *rear; } Queue;
Queue *createQueue()
{
Queue *queue = (Queue *)malloc(sizeof(Queue));
queue->front = NULL;
queue->rear = NULL;
return queue;
} void addToQueue(Queue *queue, ListNode *node)
{
if (!(queue->rear)) {
queue->rear = node;
} else {
queue->rear->next = node;
queue->rear = node;
} if (!(queue->front)) {
queue->front = node;
}
} ListNode *deleteFromQueue(Queue *queue)
{
ListNode *temp = queue->front;
if (temp) {
queue->front = queue->front->next;
return temp;
} else {
return NULL;
}
} int isEmptyQueue(Queue *queue)
{
if (queue->front == NULL) {
return ;
} else {
return ;
}
} // List Leaves
int main()
{
// 接受输入
int nodeCount;
scanf("%d", &nodeCount); ListNode a[nodeCount];
int sum = ;
for (int i = ; i < nodeCount; i++) {
char leftIndex, rightIndex;
getchar(); // 去除多余的 '\n'
scanf("%c %c", &leftIndex, &rightIndex);
ListNode input = *(ListNode *)malloc(sizeof(ListNode));
input.leftIndex = leftIndex;
input.rightIndex = rightIndex;
input.left = NULL;
input.right = NULL;
input.value = i;
a[i] = input;
sum += (leftIndex == '-' ? : leftIndex - '') + (rightIndex == '-' ? : rightIndex - '');
} // 建树
for (int i = ; i < nodeCount; i++) {
ListNode *node = &(a[i]);
char leftIndex = node->leftIndex;
char rightIndex = node->rightIndex;
node->left = leftIndex != '-' ? &(a[leftIndex - '']) : NULL;
node->right = rightIndex != '-' ? &(a[rightIndex - '']) : NULL;
}
// 根节点下标
int rootIndex = (nodeCount - ) * nodeCount / - sum;
ListNode *root = &a[rootIndex]; // 层次遍历 遇到叶节点输出
Queue *queue = createQueue();
addToQueue(queue, root);
int flag = ;
while (!isEmptyQueue(queue)) {
ListNode *node = deleteFromQueue(queue);
if (!(node->left) && !(node->right)) {
if (flag) {
printf("%d", node->value);
flag = ;
} else {
printf(" %d", node->value);
}
}
if (node->left) {
addToQueue(queue, node->left);
}
if (node->right) {
addToQueue(queue, node->right);
}
}
}
运行结果:
最新文章
- Kafka 文档引言
- 匿名函数:Lambda表达式和匿名方法
- freebsd|odoo - 为odoo报表 安装文泉译中文字体
- C#转C++的一点分享
- P1574: [Usaco2009 Jan]地震损坏Damage
- Javacript中(function(){})() 与 (function(){}()) 区别 {转}
- PHP中的一个很好用的文件上传类
- Sublime 3 and Python
- &#39;xcopy&#39; &#39;ipconfig&#39;。。。 不是内部或外部命令,也不是可运行的程序 或批处理文件
- jquery不兼容input的change事件处理
- Socket断开不报错(Java)
- Qt跨平台开发Wince5.0和Android程序
- You Only Look Once: Unified, Real-Time Object Detection(翻译)
- SR-IOV虚拟机的MTU与物理网卡的MTU
- 推荐一篇文章 《为什么C语言不会过时?》
- for循环语句个人小结
- jmeter 之 BeanShell PostProcessor跨线程全局变量使用
- Linux 目录结构说明
- Discuz的一处越权操作,强制回复无权限帖子
- 372. Delete Node in a Linked List【LintCode java】