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:
- Use a double-ended queue (deque) to keep track of the current level of nodes.
- Use a variable isLeftToRight to determine the direction of traversal. We start from left-to-right for the first level.
- 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.
- 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;
}