Leetcode 107.二叉树的层序遍历

1.题目要求:

给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

举例:
在这里插入图片描述
2.此题思路:使用层序遍历,把结点值存入二维数组中
3.做题步骤:
(1).先创建队列和出队函数,入队函数

//创建队列
typedef struct queue{
    struct TreeNode* data;
    struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
    queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
    newnode->data = data;
    newnode->next = NULL;
    if(*head == NULL){
        *head = newnode;
        return;
    }
    queue_t* tail = *head;
    while(tail->next != NULL){
        tail = tail->next;
    }
    tail->next = newnode;
}
//出队
struct TreeNode* pop(queue_t** head){
    struct TreeNode* x = (*head)->data;
    (*head) = (*head)->next;
    return x;
    
}

(2).再创两个数组,一个存入层序遍历的结点值,一个存入树中每行的结点数:

returnSize = 0;
    //判断是否为空,如果为空,则返回空指针
    if(root == NULL){
        return NULL;
    }
    int count = 1;
    int nextcount = 0;
    int depth = 0;
    queue_t* enquence = NULL;
    int size = 0; 
    int* number = (int*)malloc(sizeof(int) * 2000);//设置层序遍历数组
    int j = 0;
    int* one_line_count = (int*)malloc(sizeof(int) * 2000);//存入每层的结点数
    int j_1 = 0;
    push(&enquence,root);
    size++;

(3).然后开始层序遍历:

//开始层序遍历
    while(size != 0){
        depth++;
        for(int i = 0;i < count;i++){
            struct TreeNode* temp = pop(&enquence);
            number[j] = temp->val;
            j++;
            size--;
            if(temp->left != NULL){
                push(&enquence,temp->left);
                nextcount++;
                size++;
            }
            if(temp->right != NULL){
                push(&enquence,temp->right);
                nextcount++;
                size++;
            } 
        }
        one_line_count[j_1] = count;
        j_1++;
        count = nextcount;
        nextcount = 0;;
    }

(4).创建二维数组:

//创立二维数组
    int** node = (int**)malloc(sizeof(int*) * j_1);
    for(int i = 0;i < j_1;i++){
        node[i] = (int*)malloc(sizeof(int) * one_line_count[i]);
    }

(5).往里面存入值:

 //往里面存入值
    for(int i = 0;i < j_1;i++)
    {
        for(int j = 0;j < one_line_count[i];j++){
            node[i][j] = number[f];
            f++;
        }
    }

4.以下位全部代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
//创建队列
typedef struct queue{
    struct TreeNode* data;
    struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
    queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
    newnode->data = data;
    newnode->next = NULL;
    if(*head == NULL){
        *head = newnode;
        return;
    }
    queue_t* tail = *head;
    while(tail->next != NULL){
        tail = tail->next;
    }
    tail->next = newnode;
}
//出队
struct TreeNode* pop(queue_t** head){
    struct TreeNode* x = (*head)->data;
    (*head) = (*head)->next;
    return x;
    
}
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
    *returnSize = 0;
    //判断是否为空,如果为空,则返回空指针
    if(root == NULL){
        return NULL;
    }
    int count = 1;
    int nextcount = 0;
    int depth = 0;
    queue_t* enquence = NULL;
    int size = 0; 
    int* number = (int*)malloc(sizeof(int) * 2000);//设置层序遍历数组
    int j = 0;
    int* one_line_count = (int*)malloc(sizeof(int) * 2000);//存入每层的结点数
    int j_1 = 0;
    push(&enquence,root);
    size++;
    //开始层序遍历
    while(size != 0){
        depth++;
        for(int i = 0;i < count;i++){
            struct TreeNode* temp = pop(&enquence);
            number[j] = temp->val;
            j++;
            size--;
            if(temp->left != NULL){
                push(&enquence,temp->left);
                nextcount++;
                size++;
            }
            if(temp->right != NULL){
                push(&enquence,temp->right);
                nextcount++;
                size++;
            } 
        }
        one_line_count[j_1] = count;
        j_1++;
        count = nextcount;
        nextcount = 0;;
    }
    //创立二维数组
    int** node = (int**)malloc(sizeof(int*) * j_1);
    for(int i = 0;i < j_1;i++){
        node[i] = (int*)malloc(sizeof(int) * one_line_count[i]);
    }
    int f = 0;
    //往里面存入值
    for(int i = 0;i < j_1;i++)
    {
        for(int j = 0;j < one_line_count[i];j++){
            node[i][j] = number[f];
            f++;
        }
    }
    *returnSize = j_1;
    *returnColumnSizes = (int*)malloc((*returnSize) * sizeof(int));
    for(int i = 0;i < (*returnSize);i++){
        (*returnColumnSizes)[i] = one_line_count[i];
    }
    return node;
}

如果各位大佬觉得好的话,可以给个免费的赞吗?谢谢各位大佬了^ _ ^

  • 25
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值