03-树2 List Leaves

来源:互联网 发布:p图卖萌软件 编辑:程序博客网 时间:2024/05/20 01:11

03-树2 List Leaves   (25分)

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 NN (\le 1010) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1N1. Then NN 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


#include<stdio.h>#include<stdlib.h>//这道题是要找到叶节点,对于树来说,叶节点就是既没有左节点也没有右节点,//那么,就不能采用先序遍历,中序遍历和后序遍历 ,//因为我们需要对一个节点在访问左节点后立马访问右节点来进行判断 //所以采用层序遍历//层序遍历采用队列的存储方式 #define MaxTree 10#define ElementType int#define Tree int#define Null -1#define ERROR -2 struct TreeNode{ElementType Element;Tree Left;Tree Right;}T1[MaxTree]; //BinTree Struct顺序存储 typedef int Position;struct QNode {    ElementType Data[MaxTree];     /* 存储元素的数组 */    Position Front, Rear;  /* 队列的头、尾指针 */      };typedef struct QNode *Queue;//队列的顺序存储 Queue CreateQueue(  )//队列创建 {    Queue Q = (Queue)malloc(sizeof(struct QNode));    Q->Front = Q->Rear = 0;    return Q;} void AddQ( Queue Q, ElementType X )//队列添加元素 {    if( (Q->Rear+1 ) % MaxTree == Q->Front ) {        printf("队列满");        return ;    }    else {        Q->Rear = (Q->Rear+1)%MaxTree;//顺环队列 ,所以需要模 MaxSixe         Q->Data[Q->Rear] = X;    }} int IsEmptyQ( Queue Q )//队列是否空? {    return (Q->Front == Q->Rear);} ElementType DeleteQ( Queue Q )//队列元素删除 {    if ( IsEmptyQ(Q) ) {         printf("队列空");        return ERROR;    }    else  {        Q->Front =(Q->Front+1)%MaxTree;        return  Q->Data[Q->Front];    }} Tree BuildTree(struct TreeNode T[]){int  N,i, Root = Null;//ROOT=-1是为了空表的判断 //freopen("test.txt", "r", stdin); int check[MaxTree] ;char cl, cr; scanf("%d\n",&N);//printf("%d\n",N);//debugif(N){for(i = 0; i < N; i++){check[i] = 0;//检测是不是根结点 ,有指向的,就被设为1,就不是根节点 }for(i = 0; i < N; i++){//读数据,用字符读入方式T[i].Element = i; scanf("%c %c\n",&cl,&cr);if(cl != '-') {//左边的处理 T[i].Left = cl - '0';//字符之间相减正好得到cl对应数字的ASCII码 check[T[i].Left] = 1; }else{T[i].Left = Null;}if(cr != '-') {//右边的处理 T[i].Right = cr - '0';//字符之间相减正好得到cl对应数字的ASCII码 check[T[i].Right] = 1; }else{T[i].Right = Null;}}for(i = 0; i < N; i++){if(!check[i])break; //判断根节点 ,检测是不是根结点 ,有指向的,就被设为1,就不是根节点 } Root = i;}return Root;//树根,哪个节点没有节点指向它,就是根节点 } void LevelOrderTraversal(Tree Root){Queue Q;Tree root;int flag = 1;//用来改变输出,因为第一个叶节点和后面的输出不一样 root = Root;if(root == Null){//空树 return;}Q = CreateQueue();//创建并初始化队列AddQ(Q,root);while(!IsEmptyQ(Q)){root = DeleteQ(Q);//*****************************if((T1[root].Left == Null)&&(T1[root].Right == Null)){//判断是叶子节点 if(flag)//是第一个节点吗?{flag = 0;printf("%d",T1[root].Element); }else{printf(" %d",T1[root].Element); } } //******************************if(T1[root].Left != Null) { AddQ(Q,T1[root].Left);}if(T1[root].Right != Null) {AddQ(Q,T1[root].Right);}} } int main(){Tree BT; //输入树BT = BuildTree(T1);//层序遍历后输出叶子节点LevelOrderTraversal(BT); return 0;} 



0 0
原创粉丝点击