<PTA> Lists Leaves

来源:互联网 发布:php微信开发教程 编辑:程序博客网 时间:2024/06/05 16:33

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 integerN (10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 toN1. 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:

81 -- -0 -2 7- -- -5 -4 6

Sample Output:

4 1 5

题目大致意思是,给定整数N,代表树有N个节点,接下来的每行分别代表树的左右节点标号,“-”表示没有对应子树,要求找出该二叉树的叶子结点。显然叶子结点的左右子树

都是"-",还要求按照从上到下,从左到右的顺序输出,这个可以用层次遍历解决,详细看代码吧。

#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#define MAXSIZE 10 #define Null -1#define Tree inttypedef struct TNode BinTree;// 节点的结构struct TNode{    int index;    Tree left;    Tree right;}T[MAXSIZE];typedef struct QNode *Queue;struct QNode{    BinTree *data;    int front;    int rear;    int MaxSize;};Tree createTree(BinTree T[]);bool AddQ(Queue Q, BinTree T);BinTree DeleteQ(Queue Q);bool isEmpty(Queue Q);void LevelOrderTravelsal(Tree R);Queue createQueue(int maxsize);int main(void){    Tree R = createTree(T);    LevelOrderTravelsal(R);    return 0;}Tree createTree(BinTree T[]){// 构造二叉树,关键是要找到树的根节点(第一个输入的不一定是根节点),怎么找呢?根节点的标号必定不出现在任何一组数据// 输入数据中,嗯?上篇树的同构写过了,这里都不写了    Tree root = Null;    int N, i;    char cl, cr;    int check[MAXSIZE];    scanf("%d\n", &N);    if(N > 0){        for(i = 0; i < N; i++) check[i] = 0;        for(i = 0; i < N; i++){            T[i].index = i;            scanf("%c %c\n", &cl, &cr);            if(cl != '-'){                T[i].left = cl-'0';                check[T[i].left] = 1;            }else{                T[i].left = Null;            }            if(cr != '-'){                T[i].right = cr-'0';                check[T[i].right] = 1;            }else{                T[i].right = Null;            }        }        for(i = 0; i < N; i++){            if(!check[i]){                root = i; break;            }        }    }    return root;}Queue createQueue(int maxsize){    Queue Q = (Queue)malloc(sizeof(struct QNode));    Q->data = (BinTree*)malloc(sizeof(BinTree)*maxsize);    Q->front = Q->rear = 0;    Q->MaxSize = maxsize;    return Q;}bool AddQ(Queue Q, BinTree T){    Q->rear = (Q->rear+1)%Q->MaxSize;    Q->data[Q->rear] = T;    return true;}BinTree DeleteQ(Queue Q){    Q->front = (Q->front+1)%Q->MaxSize;    return Q->data[Q->front];}bool isEmpty(Queue Q){    return Q->front == Q->rear;}void LevelOrderTravelsal(Tree R){    // 层次遍历    int flag = 0; // 题目要求每个输入用一个空格隔开,并且不能有多余的空格,这里用flag标记输出的是不是第一个    // 不是的话在输出前输出一个空格.    BinTree BT;    if(R == Null)        return;    else{        Queue Q = createQueue(MAXSIZE+1); // 队列能容下最多的元素是开辟的空间减一,否则队列空,队列满就不好判断了        AddQ(Q, T[R]);        while(!isEmpty(Q)){            BT = DeleteQ(Q);            if(BT.left == Null && BT.right == Null){                if(flag) printf(" ");                printf("%d", BT.index);                flag++;            }            if( BT.left != Null ) AddQ(Q, T[BT.left]);            if( BT.right != Null ) AddQ(Q, T[BT.right] );        }    }}


原创粉丝点击