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;
}
相关推荐
游是水里的游38 分钟前
【算法day19】回溯:分割与子集问题
算法
不想当程序猿_39 分钟前
【蓝桥杯每日一题】分糖果——DFS
c++·算法·蓝桥杯·深度优先
graceyun41 分钟前
C语言初阶习题【9】数9的个数
c语言·开发语言
南城花随雪。1 小时前
单片机:实现FFT快速傅里叶变换算法(附带源码)
单片机·嵌入式硬件·算法
dundunmm1 小时前
机器学习之scikit-learn(简称 sklearn)
python·算法·机器学习·scikit-learn·sklearn·分类算法
古希腊掌管学习的神1 小时前
[机器学习]sklearn入门指南(1)
人工智能·python·算法·机器学习·sklearn
波音彬要多做1 小时前
41 stack类与queue类
开发语言·数据结构·c++·学习·算法
KpLn_HJL2 小时前
leetcode - 2139. Minimum Moves to Reach Target Score
java·数据结构·leetcode
Schwertlilien2 小时前
图像处理-Ch5-图像复原与重建
c语言·开发语言·机器学习
程序员buddha3 小时前
C语言从入门到放弃教程
c语言·开发语言