429. N-ary Tree Level Order Traversal
Given an n-ary tree, return the level order traversal of its nodes’ values.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Example 1:
Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]
Example 2:
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
Constraints:
- The height of the n-ary tree is less than or equal to 1000
- The total number of nodes is between [ 0 , 1 0 4 ] [0, 10^4] [0,104]
From: LeetCode
Link: 429. N-ary Tree Level Order Traversal
Solution:
Ideas:
1. Node Definition:
- The struct Node contains val (the node’s value), numChildren (the number of children), and children (an array of pointers to the children nodes).
2. Queue Implementation:
- To perform a level-order traversal, we implement a simple queue using linked nodes. This helps in processing each node level by level.
3. Breadth-First Traversal:
- We enqueue the root node and start the BFS. For each level, we keep track of the number of nodes at that level (levelSize), and then process all the nodes at that level by dequeuing them and enqueuing their children.
4. Dynamic Allocation:
- We allocate memory dynamically for both the result and returnColumnSizes. After processing each level, we increment *returnSize to reflect the number of levels processed.
Code:
/**
* Definition for a Node.
* struct Node {
* int val;
* int numChildren;
* struct Node** children;
* };
*/
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
typedef struct QueueNode {
struct Node* node;
struct QueueNode* next;
} QueueNode;
typedef struct {
QueueNode* front;
QueueNode* rear;
} Queue;
// Function to initialize the queue
Queue* createQueue() {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->front = queue->rear = NULL;
return queue;
}
// Function to enqueue a node
void enqueue(Queue* queue, struct Node* node) {
QueueNode* newQueueNode = (QueueNode*)malloc(sizeof(QueueNode));
newQueueNode->node = node;
newQueueNode->next = NULL;
if (queue->rear == NULL) {
queue->front = queue->rear = newQueueNode;
return;
}
queue->rear->next = newQueueNode;
queue->rear = newQueueNode;
}
// Function to dequeue a node
struct Node* dequeue(Queue* queue) {
if (queue->front == NULL) return NULL;
QueueNode* temp = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) queue->rear = NULL;
struct Node* node = temp->node;
free(temp);
return node;
}
// Function to check if the queue is empty
int isQueueEmpty(Queue* queue) {
return queue->front == NULL;
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** levelOrder(struct Node* root, int* returnSize, int** returnColumnSizes) {
if (root == NULL) {
*returnSize = 0;
*returnColumnSizes = NULL;
return NULL;
}
// Prepare to store the results
int** result = (int**)malloc(1000 * sizeof(int*));
*returnColumnSizes = (int*)malloc(1000 * sizeof(int));
*returnSize = 0;
// Queue for BFS
Queue* queue = createQueue();
enqueue(queue, root);
while (!isQueueEmpty(queue)) {
int levelSize = 0;
int currentSize = 0;
// Get the current level size by peeking into the queue
QueueNode* temp = queue->front;
while (temp != NULL) {
levelSize++;
temp = temp->next;
}
// Store all nodes in the current level
result[*returnSize] = (int*)malloc(levelSize * sizeof(int));
(*returnColumnSizes)[*returnSize] = levelSize;
for (int i = 0; i < levelSize; i++) {
struct Node* node = dequeue(queue);
result[*returnSize][i] = node->val;
// Enqueue the children of the current node
for (int j = 0; j < node->numChildren; j++) {
enqueue(queue, node->children[j]);
}
}
(*returnSize)++;
}
// Free queue
free(queue);
return result;
}