leetcode 2415.反转二叉树的奇数层

1.题目要求:

c 复制代码
给你一棵 完美 二叉树的根节点 root ,请你反转这棵树中每个 奇数 层的节点值。

例如,假设第 3 层的节点值是 [2,1,3,4,7,11,29,18] ,那么反转后它应该变成 [18,29,11,7,4,3,1,2] 。
反转后,返回树的根节点。

完美 二叉树需满足:二叉树的所有父节点都有两个子节点,且所有叶子节点都在同一层。

节点的 层数 等于该节点到根节点之间的边数。


2.全部代码:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
//创建队列结构体
typedef struct queue{
    struct TreeNode* value;
    struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
    queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
    newnode->value = 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)->value;
    (*head) = (*head)->next;
    return x;
}
//逆置函数
void reverse(int* number,int left,int right){
    while(left <= right){
        int temp = number[left];
        number[left] = number[right];
        number[right] = temp;
        left++;
        right--;
    }
}
struct TreeNode* reverseOddLevels(struct TreeNode* root) {
    queue_t* quence = NULL;
    int nextcount = 0;
    int count = 1;
    int depth = -1;
    int* number = (int*)malloc(sizeof(int) * 20000);//层序遍历的数组
    int j = 0;
    int index = 0;//记录每一层的起点索引
    int size = 0;
    //开始层序遍历
    push(&quence,root);
    size++;
    while(size != 0){
        depth++;
        for(int i = 0;i < count;i++){
            struct TreeNode* temp = pop(&quence);
            number[j] = temp->val;//把层序遍历的节点值存入数组中
            j++;
            size--;
            if(temp->left != NULL){
                push(&quence,temp->left);
                size++;
                nextcount++;
            }
            if(temp->right != NULL){
                push(&quence,temp->right);
                size++;
                nextcount++;
            }
        }
        //如果高度是偶数,则起点索引加上这一层的结点数,变为下一层的起点索引
        if(depth % 2 == 0){
            index += count;
        }else{
            reverse(number,index,j - 1);//如果高度是奇数,则把这一层进行逆置
            index += count;
        }
        //把下一层的结点数赋给count;
        count = nextcount;
        nextcount = 0;
    }
    size = 0;
    int i = 0;
    push(&quence,root);
    size++;
    //再进行一次层序遍历,把数组的值反过来,赋给树中的结点
    while(size != 0){
        struct TreeNode* temp = pop(&quence);
        size--;
        temp->val = number[i];
        i++;
        if(temp->left != NULL){
            push(&quence,temp->left);
            size++;
        }
        if(temp->right != NULL){
            push(&quence,temp->right);
            size++;
        }
    }
    return root;
}

全部步骤都标在代码中,大家如果觉得好的话,不妨给个免费的赞吧,谢谢了^ _ ^

相关推荐
不二狗8 分钟前
每日算法 -【Swift 算法】查找字符串数组中的最长公共前缀
开发语言·算法·swift
不二狗12 分钟前
每日算法 -【Swift 算法】将整数转换为罗马数字
开发语言·算法·swift
Moonbit24 分钟前
双周报Vol.73:移除使用方法实现 trait 、新增了 “错误多态” 功能、.语法支持使用 _ 的匿名函数...
后端·算法
chao_78944 分钟前
链表题解——反转链表【LeetCode】
开发语言·python·算法
Code_流苏1 小时前
Python趣学篇:从零打造智能AI井字棋游戏(Python + Tkinter + Minimax算法)
python·算法·游戏·tkinter·智能井字棋·minimax算法
理智的灰太狼1 小时前
题目 3230: 蓝桥杯2024年第十五届省赛真题-星际旅行
算法·职场和发展·蓝桥杯
wcjwdq1 小时前
“顶点着色器”和“片元着色器”是先处理完所有顶点再统一进入片元阶段,还是一个顶点处理完就去跑它的片元?
算法·着色器
技术帮扶户2 小时前
Leetcode-7 寻找用户推荐人
算法·leetcode·职场和发展
VU-zFaith8702 小时前
C++概率论算法详解:理论基础与实践应用
c++·算法·概率论
全栈凯哥2 小时前
Java详解LeetCode 热题 100(23):LeetCode 206. 反转链表(Reverse Linked List)详解
java·算法·leetcode·链表