题目:

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的更多相关文章

  1. [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 ...

  2. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  3. 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 ...

  4. LeetCode Sum of Left Leaves

    原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...

  5. 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 ...

  6. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. 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 ...

  8. 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...

  9. 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 ...

随机推荐

  1. sprintf、vsprintf、sprintf_s、vsprintf_s、_snprintf、_vsnprintf、snprintf、vsnprintf 函数辨析

    看了题目中的几个函数名是不是有点头晕?为了防止以后总在这样的细节里纠缠不清,今天我们就来好好地辨析一下这几个函数的异同. 实验环境: Windows下使用VS2017Linux下使用gcc4.9.4 ...

  2. JRE与JVM、JDK的区别

    JRE与JVM.JDK的区别 一. 详细介绍1.JVM -- java virtual machine JVM就是我们常说的java虚拟机,它是整个java实现跨平台的 最核心的部分,所有的java程 ...

  3. 实现toggleClass功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. iOS程序发布测试-生成ad hoc证书

    转自: http://blog.sina.com.cn/s/blog_68444e230100srdn.html iOS程序发布测试3-生成ad hoc证书 iOS证书分2种,1种是开发证书,用来给你 ...

  5. 遍历JavaScript某个对象所有的属性名称和值

    /* * 用来遍历指定对象所有的属性名称和值 * obj 需要遍历的对象 */ function allPrpos(obj) { // 用来保存所有的属性名称和值 var props = " ...

  6. {...formItemLayout} 标签布局

    {...formItemLayout}是reactjs中属性的写法{...props},formItemLayout标签布局,wrapperCol需要为输入控件设置布局样式时,和label 标签布局, ...

  7. 关于Future.cancel(mayInterruptIfRunning)方法的参数的问题

    mayInterruptIfRunning设成false话,不允许在线程运行时中断,设成true的话就允许. 可以参考下面的代码来理解,如果设为false的话,会打印到99999,如果设成true的话 ...

  8. Python之print详解

    Python之print详解 http://www.jb51.net/article/55768.htm   print的一些基本用法,在前面的讲述中也涉及一些,本讲是在复习的基础上,尽量再多点内容. ...

  9. 安装Redmine 2.3.0(Ubuntu 12.04 Server)

    怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ 安装Redmine 2.3.0(Ubuntu 12.04 Server) 翻译源\参考源 ...

  10. Js随机生成指定长度字符串

    function a(a) { var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&q ...