C语言 03-树2 List Leaves

该博客介绍了如何使用C语言解决在给定树结构中层次遍历并按特定顺序打印所有叶子节点的问题。首先,通过输入数据建立静态链表结构的树,然后利用队列进行层次遍历,遇到叶节点时打印其索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Show me the Code:

#include <stdio.h>
#include <stdlib.h>
#ifndef NULL
#define NULL 0
#endif // NULL
#define MAXSIZE 10
#define Null -1

typedef int ElementType;
struct QNode
{
    ElementType Data;
    struct QNode *Next;
};
typedef struct QNode *Position;
typedef Position Queque;

struct TNode
{
    ElementType Data;
    int left;
    int right;
} T[MAXSIZE];

Queque CreateQueue();
int add(Queque q, ElementType e);
ElementType Delete(Queque q);
int IsEmpty(Queque q);
int BuildTree();
void LevelOrderTraversal(int root);

int main()
{
    int root;
    root = BuildTree();
    LevelOrderTraversal(root);
    return 0;
}


/*队列抽象数据结构*/
Queque CreateQueue()
{
    Queque q;
    q = (Queque)malloc(sizeof(struct QNode));
    q->Data = 0;
    q->Next = NULL;
    return q;
}

int add(Queque q, ElementType e)
{
    Position p,tmp;
    if(!q)
        q = CreateQueue();
    tmp = q;
    while(tmp->Next)
    tmp = tmp->Next;
    p = (Position)malloc(sizeof(struct QNode));
    p->Data = e;
    p->Next = NULL;
    tmp->Next = p;
    return 0;
}

ElementType Delete(Queque q)
{
    ElementType First;
    Position tmp;
    if(!q || q->Next == NULL)
        return NULL;
    else
    {
        tmp = q->Next;
        q->Next = tmp->Next;
        First = tmp->Data;
        free(tmp);
        return First;
    }
}
int IsEmpty(Queque q)
{
    return(q->Next == NULL);
}

/*二叉树静态链表数据结构*/

int BuildTree()
{
    int N;
    scanf("%d",&N);
    getchar();
    if(N)
    {
        int i,root;
        int check[N];
        char cl,cr;
        for(i=0;i<N;i++)
            check[i] = 0;
        for(i=0;i<N;i++)
        {
            scanf("%c %c",&cl,&cr);
            getchar();
            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]==0){
                root = i;
                break;
            }
        }
        return root;
    }
    else
        return Null;
}

void LevelOrderTraversal(int root)
{
    Queque q;
    q = CreateQueue();
    if(root<0)
        return;
    int tmp,flag = 1;
    add(q,root);
    while(!IsEmpty(q))
    {
        tmp = Delete(q);
        if(T[tmp].left == Null && T[tmp].right == Null)
        {
            if(flag)
            {
                printf("%d",tmp);
                flag = 0;
            }
            else
                printf(" %d",tmp);
        }
        else if(T[tmp].left == Null)
            add(q,T[tmp].right);
        else if(T[tmp].right == Null)
            add(q,T[tmp].left);
        else
        {
            add(q,T[tmp].left);
            add(q,T[tmp].right);
        }
    }
}

结果

这里写图片描述

思路与问题

问题不难,包括两个部分,建树和层次遍历找叶节点。
在树的同构中我们学习了通过静态链表建树的方法,此处我们同样通过建立一个结构体数组,将输入内容先存下来,通过结构所包含的left和right变量指向儿子节点的数组下标。
层次遍历部分,不能通过递归实现,在mooc中,何钦铭老师已经讲述了通过队列实现层次遍历的方法,此处即是用这样的思路对树进行遍历,遇到没有儿子节点的结点,就将他print出来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值