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 ...
随机推荐
- JSP中的TAG文件和TLD文件小结
在jsp文件中,可以引用tag和tld文件. 1.对于tag文件 <%@ taglib prefix="ui" tagdir="/WEB-INF/tags" ...
- Android RxJava/RxAndroid结合Retrofit使用
概述 RxJava是一个在 Java VM 上使用可观測的序列来组成异步的.基于事件的程序的库.更重要的是:使用RxJava在代码逻辑上会非常简洁明了,尤其是在复杂的逻辑上.告别迷之缩进. RxAnd ...
- 如何使用Apache设置404页面
方法一:[.htaccess文件配置404] 网上大部分解决办法是:首先你要开启Apache的rewrite_module模块,支持.htaccess,然后在网站根目录建立.htaccess文件(或已 ...
- UIView的transform属性
一.什么是Transform Transform(变化矩阵)是一种3×3的矩阵,如下图所示: 通过这个矩阵我们可以对一个坐标系统进行缩放,平移,旋转以及这两者的任意组着操作.而且矩阵的操作不具备交换律 ...
- oracle update left join查询
对于有的更新语句,要更新的表可能条件不够,需要用到left join关联其他表, 但是不能直接关联,否则报错:错误如下: update imim_gireqbillitems gi left join ...
- 微信小程序之下拉刷新,上拉更多列表实现
代码地址如下:http://www.demodashi.com/demo/11110.html 一.准备工作 首先需要下载小程序开发工具 官方下载地址: https://mp.weixin.qq.co ...
- jboss部署web应用
http://liufei-fir.iteye.com/blog/759772初次部署jboss的web应用,把tomcat/weblogic下的工程移植到jboss上发布 一.修改JBOSS应用服务 ...
- linux ps查看进程命令详解
http://linux.net527.cn/Linuxwendang/xitongguanliyuan/39094.htmlLinux操作系统PS命令详细解析 要对系统中进程进行监测控制,用 ps ...
- unity3d移动及键鼠状态
gameObject的transform属性可以进行位置.旋转.大小的设置 位置:position,Translate(),位置的移动 旋转:rotate 大小:localScale Vector3内 ...
- Solr3.6.2和Solr4.9.0经常使用配置
tomcat 以tomcat 7为例,位置/work/apache-tomcat-7.0.55 Solr 3.6.2 基本配置 Solr 3.6.2.须要JDK 6/JDK7支持. 下载Solr 3. ...