题目:

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);
}
}
}

运行结果:

最新文章

  1. Kafka 文档引言
  2. 匿名函数:Lambda表达式和匿名方法
  3. freebsd|odoo - 为odoo报表 安装文泉译中文字体
  4. C#转C++的一点分享
  5. P1574: [Usaco2009 Jan]地震损坏Damage
  6. Javacript中(function(){})() 与 (function(){}()) 区别 {转}
  7. PHP中的一个很好用的文件上传类
  8. Sublime 3 and Python
  9. &#39;xcopy&#39; &#39;ipconfig&#39;。。。 不是内部或外部命令,也不是可运行的程序 或批处理文件
  10. jquery不兼容input的change事件处理
  11. Socket断开不报错(Java)
  12. Qt跨平台开发Wince5.0和Android程序
  13. You Only Look Once: Unified, Real-Time Object Detection(翻译)
  14. SR-IOV虚拟机的MTU与物理网卡的MTU
  15. 推荐一篇文章 《为什么C语言不会过时?》
  16. for循环语句个人小结
  17. jmeter 之 BeanShell PostProcessor跨线程全局变量使用
  18. Linux 目录结构说明
  19. Discuz的一处越权操作,强制回复无权限帖子
  20. 372. Delete Node in a Linked List【LintCode java】

热门文章

  1. meta文件是什么东西
  2. mac 查看目前哪些进程占用哪些端口
  3. B2C电子商务系统研发——产品媒体常见功能点
  4. Unix 网络编程 dup和dup2函数
  5. 使用SQL命令查看MYSQL数据库大小
  6. Service stopSelf(int statId)和onStartcommand(Intent intent,int flags,int startId)
  7. python代码 构建验证码
  8. linux按内容查找文件
  9. 记一下吧,又记不住啦。pipe
  10. C# Winform 实现自定义半透明遮罩层介绍