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;
}
相关推荐
算AI18 小时前
人工智能+牙科:临床应用中的几个问题
人工智能·算法
似水এ᭄往昔19 小时前
【C语言】文件操作
c语言·开发语言
hyshhhh20 小时前
【算法岗面试题】深度学习中如何防止过拟合?
网络·人工智能·深度学习·神经网络·算法·计算机视觉
蒙奇D索大20 小时前
【数据结构】第六章启航:图论入门——从零掌握有向图、无向图与简单图
c语言·数据结构·考研·改行学it
杉之21 小时前
选择排序笔记
java·算法·排序算法
烂蜻蜓21 小时前
C 语言中的递归:概念、应用与实例解析
c语言·数据结构·算法
OYangxf21 小时前
图论----拓扑排序
算法·图论
我要昵称干什么21 小时前
基于S函数的simulink仿真
人工智能·算法
AndrewHZ1 天前
【图像处理基石】什么是tone mapping?
图像处理·人工智能·算法·计算机视觉·hdr
念九_ysl1 天前
基数排序算法解析与TypeScript实现
前端·算法·typescript·排序算法