数据结构基础— List Leaves

本文介绍了一种通过层次遍历二叉树来找出所有叶子节点的方法,并提供了一个C语言实现的例子。输入包括树的节点数量及各节点的左右子节点信息,输出则按照从上至下、从左至右的顺序列出所有叶子节点的索引。

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

从上到下,从左到右输出,就是层次遍历
所有的查找什么东西在树中 都是遍历的问题
这里是查找叶节点
那就是 “层次遍历 + 判断是否为叶节点”
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

//
//  main.c
//  List leaves
//
//  Created by air on 2018/3/21.
//
#define Null -1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int check[15]; /* 最大是10,所以我定个15绰绰有余了,而且队列也好判断 */
int root,N;
int store[15];
typedef struct Node{
    int index;
    int left;
    int right;
}treeNode;

typedef struct Qnode{
    treeNode Tree [15];
    int front;
    int rear;
} my_queue;

typedef my_queue * ptrToQueue;
typedef ptrToQueue queue;

void makeEmpty(ptrToQueue q){
    q->front = 0;
    q->rear = 0;
}

void pop(ptrToQueue q){
    q->front ++;
}

int top(ptrToQueue q){
    return q->Tree[q->front].index;
}

void push(ptrToQueue q, treeNode x){
    q->Tree[(q->rear)] = x;
    q->rear ++;
}

bool isEmpty(queue q){
    return q->rear == q->front;
}

/* read funxtion test fine */
void readQueue(treeNode arrayOfTree[]){
    scanf("%d\n",&N);
    char lc,rc;
    for(int i = 0; i < 15; i++){
        check[i] = 0;
        store[i] = 0;
    }
    for(int i = 0; i < N; i++){
        arrayOfTree[i].index = i;
        if(i == N-1)
            scanf("%c %c", &lc, &rc);         /* the last line of input is different */
        else
            scanf("%c %c\n", &lc, &rc);
        if(lc != '-'){
            arrayOfTree[i].left = lc -'0';    /* set the left number of tree*/
            check[arrayOfTree[i].left] = 1;   /* set the number of check*/
        }
        else
            arrayOfTree[i].left = Null;
        if(rc != '-'){
            arrayOfTree[i].right = rc -'0';
            check[arrayOfTree[i].right] = 1;
        }
        else
            arrayOfTree[i].right = Null;
    }
}

void findRoot(void){
    for (int i = 0; i < N; i ++) {
        if(check[i] == 0)
            root = i;
    }
}
/* level order traversal */
void levelOrderTraversal(queue q, treeNode arrayOfTree[]){  //混乱- -
    int count = 0;
    push(q, arrayOfTree[root]);
    while (!isEmpty(q)) { /*是不是很像BFS- -  我觉得层次遍历就是个BFS*/
        int topIndex = top(q);
        pop(q);
        if(arrayOfTree[topIndex].left == Null && arrayOfTree[topIndex].right == Null){
            store[count++] = topIndex;
            continue;
        }
        if(arrayOfTree[topIndex].left != Null)
            push(q, arrayOfTree[arrayOfTree[topIndex].left]);
        if(arrayOfTree[topIndex].right != Null)
            push(q, arrayOfTree[arrayOfTree[topIndex].right]);
    }
    /* 因为很奇葩的输出方式,我只好把结果先出存在 store 这个数组这里*/
    for(int i = 0; i < count-1; i++)
        printf("%d ",store[i]);
    printf("%d",store[count-1]);
}

int main(int argc, const char * argv[]) {
    queue q = (ptrToQueue)malloc(sizeof(my_queue));
    makeEmpty(q);
    treeNode arrayOfTree[15];
    readQueue(arrayOfTree); /* 读入数组 */
    findRoot(); /* 找到根节点 */
    levelOrderTraversal(q,arrayOfTree); /* 层次遍历并打印 */
    free(q);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值