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 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(末尾没有空格, 中间有空格)

解答:

目的:找到叶节点,从上到下,从左到右输出。
输入:给出节点数以及每个节点左右的子树位置
方法
1 获取节点数目,存储节点,找到根节点
2 层序遍历,查看节点左右子树,左右子树为空则打印节点位置。

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define MaxTree 10  //最大的N<=10
#define Null -1

typedef struct Nqueue* pQueue;   
struct Nqueue{           //序列节点,顺环序列
	int Data[MaxTree];//序列的长度为MaxTree值-1,
                      //由于顺环序列,为了判断序列空而少用一个。
	int front;        //指向序列头节点的前一个位置
	int rear;         //指向序列尾节点
};
//使用静态链表的的方式,结构结构
struct TreeNode{  
    char element;   //二叉树的节点结构,本题中可以不用element
    int left;
    int right;
}T1[MaxTree];       //创建结构数组
/**
 * 建二叉树
 * 返回值:树根
*/
int BuildTree(struct TreeNode T[]){
    int num,i ,treeroot=Null;
    char cl,cr;
    int check[10]={0,};             //为了寻找根节点,开始全为0
    scanf("%d",&num);
    for(i =0;i<num;i++){
    scanf("\n%c %c",&cl,&cr);   //输入前边加\n巧妙避免结尾的\n使程序无法正常结束,
                                //还能够补偿输入num的回车。
    if(cl!='-') {
        T[i].left = cl - '0';
        check[T[i].left] = 1;   //有说明不是根节点,对应的位置置一
    }else T[i].left = Null;     //对应位值为-1
    if(cr!='-') {
        T[i].right = cr - '0';
        check[T[i].right] = 1;
    }else T[i].right = Null;
    }
    for(i=0 ;i<num ;i++){      //遍历寻找根节点
        if(!check[i]){
            treeroot = i;
            break;
        }
    }
    return treeroot;
}
pQueue CreatQ(){
    pQueue queue = (pQueue)malloc(sizeof(struct Nqueue));    //序列指针,需要申请空间。
    queue->front = 10;     //顺环序列,空序列,rear指向头,front指向尾
    queue->rear = 0;
    return queue;
}
/**
 * 向序列增加值
 * 判断是否满了
 * 加一取余法实现循环使用
*/
void addQ(pQueue q,int item){  
    if((q->rear+1)%MaxTree == q->front) return;
    q->rear=(q->rear+1)%MaxTree;
    q->Data[q->rear] = item; 

}
/**
 * 判断是否为空
*/
bool IsE(pQueue q){
    bool ret =0;
    if(q->front == q->rear) ret = 1;
    return ret;
}
/**
 * 取出序列值
 * 判断是否空
*/
int delQ(pQueue q){
    if(IsE(q)) return -1;
    q->front = (q->front+1)%MaxTree;
    return q->Data[q->front];
}
/**
 * 判断是否为叶节点
 * 利用序列特性:先进先出。实现层序遍历。
 * 判断序列是否为空,不为空取出第一个值,判断其左右子树,不空则继续加进序列中
 * 若空,则为子树,打印。
*/
int JudgeLeave(struct TreeNode BT[],int root){
    int node;
    if(root ==-1) return;
    pQueue q = CreatQ();
    addQ(q,root);
    while (!IsE(q))
    {
        node = delQ(q);
        if(BT[node].left ==Null&&BT[node].right==Null){
            printf("%d",node);
            if(!IsE(q)) printf(" ");
        }
        if(BT[node].left !=Null)
        {
            addQ(q,BT[node].left);
        }
        if(BT[node].right !=Null)
        {
            addQ(q,BT[node].right);
        }
    }
}

int main(){
    int root;
    root = BuildTree(T1);
    JudgeLeave(T1,root);
    system("pause");
    return 0;
}

总结:本次代码使用了序列的相关操作,对于静态链表的使用,通过构建结构体体数组的方式实现二叉树的存储。
  另外对于scanf函数的使用有了更深的理解,其中的空格,换行符号的使用,当scanf输入中格式末尾有\n时候,在终端运行的时候,输入到最后,再点击回车,程序是不运行的,必须随便在输入一个东西,程序才能够继续运行。因为如果结尾有回车的话,缓冲区进入的是内容和回车符号,回车前面的东西赋值给了参数,但是缓冲区并非为空,因此需要再输入一个值,才能够结束输入。

“scanf的规定,必须要有一个空白符才能够结束输入,因此还会要求继续输入”

具体可以看看:https://blog.csdn.net/weiweicsdn1/article/details/52185453
参考:https://blog.csdn.net/qq_39805362/article/details/82820642

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值