LeetCode //C - 103. Binary Tree Zigzag Level Order Traversal

103. Binary Tree Zigzag Level Order Traversal

Given the root of a binary tree, return the zigzag level order traversal of its nodes' value s. (i.e., from left to right, then right to left for the next level and alternate between).

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]

Example 2:

Input: root = [1]
Output: [[1]]

Example 3:

Input: root = []
Output: []

Constraints:
  • The number of nodes in the tree is in the range [0, 2000].
  • -100 <= Node.val <= 100

From: LeetCode

Link: 103. Binary Tree Zigzag Level Order Traversal


Solution:

Ideas:
  1. Use a double-ended queue (deque) to keep track of the current level of nodes.
  2. Use a variable isLeftToRight to determine the direction of traversal. We start from left-to-right for the first level.
  3. For each level:
    • If isLeftToRight is true, pop nodes from the front of the deque and push their left child followed by their right child to the back.
    • Otherwise, pop nodes from the back of the deque and push their right child followed by their left child to the front.
  4. Keep track of the number of nodes at the current level to determine when to switch to the next level and reverse the traversal direction.
Code:
c 复制代码
typedef struct {
    struct TreeNode **data;
    int front;
    int rear;
    int size;
    int capacity;
} Deque;

Deque* createDeque(int capacity) {
    Deque *deque = (Deque *)malloc(sizeof(Deque));
    deque->data = (struct TreeNode **)malloc(capacity * sizeof(struct TreeNode *));
    deque->front = 0;
    deque->rear = -1;
    deque->size = 0;
    deque->capacity = capacity;
    return deque;
}

void pushFront(Deque *deque, struct TreeNode *node) {
    deque->front = (deque->front - 1 + deque->capacity) % deque->capacity;
    deque->data[deque->front] = node;
    deque->size++;
}

void pushRear(Deque *deque, struct TreeNode *node) {
    deque->rear = (deque->rear + 1) % deque->capacity;
    deque->data[deque->rear] = node;
    deque->size++;
}

struct TreeNode* popFront(Deque *deque) {
    struct TreeNode *node = deque->data[deque->front];
    deque->front = (deque->front + 1) % deque->capacity;
    deque->size--;
    return node;
}

struct TreeNode* popRear(Deque *deque) {
    struct TreeNode *node = deque->data[deque->rear];
    deque->rear = (deque->rear - 1 + deque->capacity) % deque->capacity;
    deque->size--;
    return node;
}

int** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
    if (!root) {
        *returnSize = 0;
        return NULL;
    }

    int **result = (int **)malloc(2000 * sizeof(int *));
    *returnColumnSizes = (int *)malloc(2000 * sizeof(int));
    *returnSize = 0;

    Deque *deque = createDeque(2000);
    pushRear(deque, root);
    int isLeftToRight = 1;

    while (deque->size > 0) {
        int levelSize = deque->size;
        (*returnColumnSizes)[*returnSize] = levelSize;
        result[*returnSize] = (int *)malloc(levelSize * sizeof(int));

        for (int i = 0; i < levelSize; i++) {
            struct TreeNode *current;
            if (isLeftToRight) {
                current = popFront(deque);
                result[*returnSize][i] = current->val;
                if (current->left) pushRear(deque, current->left);
                if (current->right) pushRear(deque, current->right);
            } else {
                current = popRear(deque);
                result[*returnSize][i] = current->val;
                if (current->right) pushFront(deque, current->right);
                if (current->left) pushFront(deque, current->left);
            }
        }

        isLeftToRight = !isLeftToRight;
        (*returnSize)++;
    }

    free(deque->data);
    free(deque);
    return result;
}
相关推荐
Ocean☾1 分钟前
前端基础-html-注册界面
前端·算法·html
顶呱呱程序9 分钟前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
TeYiToKu21 分钟前
笔记整理—linux驱动开发部分(9)framebuffer驱动框架
linux·c语言·arm开发·驱动开发·笔记·嵌入式硬件·arm
互联网打工人no128 分钟前
每日一题——第一百二十四题
c语言
爱吃生蚝的于勒31 分钟前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
羊小猪~~34 分钟前
数据结构C语言描述2(图文结合)--有头单链表,无头单链表(两种方法),链表反转、有序链表构建、排序等操作,考研可看
c语言·数据结构·c++·考研·算法·链表·visual studio
洋2401 小时前
C语言常用标准库函数
c语言·开发语言
王哈哈^_^1 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
星沁城1 小时前
240. 搜索二维矩阵 II
java·线性代数·算法·leetcode·矩阵
脉牛杂德1 小时前
多项式加法——C语言
数据结构·c++·算法