Lists Leaves

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 N1. 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

題目大致意思是,給定整數N,代表樹有N個節點,接下來的每行分別代表樹的左右節點標號,“-”表示沒有對應子樹,要求找出該二叉樹的葉子結點。顯然葉子結點的左右子樹

都是"-",還要求按照從上到下,從左到右的順序輸出,這個可以用層次遍歷解決,詳細看代碼吧。

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAXSIZE 10 
#define Null -1
#define Tree int
typedef 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] );
        }
    }
}


發佈了42 篇原創文章 · 獲贊 12 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章