leetcode 116.填充每个节点的下一个右侧结点指针

1.题目要求:

给定一个二叉树:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL 。

初始状态下,所有 next 指针都被设置为 NULL

在这里插入图片描述
2.做题步骤:
(1)先创建好队列结构体,入队函数,出队函数:

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

(2)设置变量,进行层序遍历:

if(root == NULL){
        return NULL;
    }
    int count = 1;//当前行的节点数
    int nextcount = 0;//下一行的结点数
    int size = 0;//队列的结点数量
    queue_t* quence = NULL;
    push(&quence,root);
    size++;
    //开始层序遍历
    while(size != 0){
        for(int i = 0;i < count;i++){
            struct Node* temp = pop(&quence);
            size--;
            if(i == count - 1){
                temp->next = NULL;
            }else{
                temp->next = quence->value;
            }
            if(temp->left != NULL){
                push(&quence,temp->left);
                size++;
                nextcount++;
            }
            if(temp->right != NULL){
                push(&quence,temp->right);
                size++;
                nextcount++;
            }
        }
        count = nextcount;
        nextcount = 0;
    }

全部代码:

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *left;
 *     struct Node *right;
 *     struct Node *next;
 * };
 */
//创建队列结构体
typedef struct queue{
    struct TreeNode* value;
    struct queue* next1;
}queue_t;
//入队
void push(queue_t** head,struct Node* data){
    queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
    newnode->value = data;
    newnode->next1 = NULL;
    if(*head == NULL){
        *head = newnode;
        return;
    }
    queue_t* tail = *head;
    while(tail->next1 != NULL){
        tail = tail->next1;
    }
    tail->next1 = newnode;
}
//出队
struct Node* pop(queue_t** head){
    struct TreeNode* x = (*head)->value;
    (*head) = (*head)->next1;
    return x;
}
struct Node* connect(struct Node* root) {
    if(root == NULL){
        return NULL;
    }
    int count = 1;//当前行的节点数
    int nextcount = 0;//下一行的结点数
    int size = 0;//队列的结点数量
    queue_t* quence = NULL;
    push(&quence,root);
    size++;
    //开始层序遍历
    while(size != 0){
        for(int i = 0;i < count;i++){
            struct Node* temp = pop(&quence);
            size--;
            if(i == count - 1){
                temp->next = NULL;
            }else{
                temp->next = quence->value;
            }
            if(temp->left != NULL){
                push(&quence,temp->left);
                size++;
                nextcount++;
            }
            if(temp->right != NULL){
                push(&quence,temp->right);
                size++;
                nextcount++;
            }
        }
        count = nextcount;
        nextcount = 0;
    }
    return root;
}

好了,这就是我的全部代码了,大家如果觉得好的话,给个免费的赞吧,谢谢了^ _ ^

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值