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);
}
}
}
运行结果:

PAT003 List Leaves的更多相关文章
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- LeetCode - 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- LeetCode Sum of Left Leaves
原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...
- Leetcode: Find Leaves of Binary Tree
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 366. Find Leaves of Binary Tree
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves ...
- 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...
- Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
随机推荐
- 文本域光标操作(选、添、删、取)的jQuery扩展
; (function ($) { /* * 文本域光标操作(选.添.删.取)的jQuery扩展 @Mr.Think http://mrthink.net/text-field-jquery-exte ...
- EffectiveJava(30) -- 全面解析enum类型
--在大多数项目中,我们会经常使用int类型来声明final类型的常量,它在不考虑安全的情况下确实能满足我们绝大多数的需求.但是在JDK1.5版本发布之后,声明一组固定的常量组成合法值的类型就建议使用 ...
- SQL语句详细汇总[转]
经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...
- 用Markdown写博客快速入门
Markdown,简单来说,就是一种可以方便转换为HTML的带标记符号纯文本. 它是对我等键盘党的福音:我不用再费劲挪动鼠标去按加粗.设置段落了,用键盘输入所有文本,一气呵成. 最重要的是,cnblo ...
- 算法笔记_081:蓝桥杯练习 算法提高 矩阵乘法(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 有n个矩阵,大小分别为a0*a1, a1*a2, a2*a3, ..., a[n-1]*a[n],现要将它们依次相乘,只能使用结合率,求最 ...
- UTC时间格式转换
如‘2018-08-07T14:44:40.000+0800’时间转换为正常时间格式 使用moment库 import moment from 'moment' // 日期格式化 formatTime ...
- [XCode A] - 编程相关问题
1.如何在XCode中新建文件夹 如果在xcode工程中new group,只是在视觉效果上分好了几个文件夹,方便分类管理,但在finder中并不会创建新的文件夹,在硬盘目录还是所有文件都并列在一个文 ...
- 从cocos2dx到cocos2dhtml5的不同之处
首先cocos2dhtml5使用javascript编程, 严格区分大写和小写. 1.新建cocos2dhtml5项目. 直接复制引擎自带的helloworld.改一下目录名字就可以. 2.新增js文 ...
- Android实现截图分享qq,微信
代码地址如下:http://www.demodashi.com/demo/13292.html 前言 现在很多应用都有截图分享的功能,今天就来讲讲截图分享吧 今天涉及到以下内容: android权限设 ...
- Android--从零开始开发一款文章阅读APP
代码地址如下:http://www.demodashi.com/demo/11212.html 前言 本案例已经开源!如果你想免费下载,可以访问我的Github,所有案例均在上面,只求给个star.当 ...