C语言 | Leetcode C语言题解之第429题N叉树的层序遍历
题目:
题解:
#define MAX_LEVE_SIZE 1000
#define MAX_NODE_SIZE 10000int** levelOrder(struct Node* root, int* returnSize, int** returnColumnSizes) {int ** ans = (int **)malloc(sizeof(int *) * MAX_LEVE_SIZE);*returnColumnSizes = (int *)malloc(sizeof(int) * MAX_LEVE_SIZE);if (!root) {*returnSize = 0;return ans;}struct Node ** queue = (struct Node **)malloc(sizeof(struct Node *) * MAX_NODE_SIZE);int head = 0, tail = 0;int level = 0;queue[tail++] = root;while (head != tail) {int cnt = tail - head;ans[level] = (int *)malloc(sizeof(int) * cnt);for (int i = 0; i < cnt; ++i) {struct Node * cur = queue[head++];ans[level][i] = cur->val;for (int j = 0; j < cur->numChildren; j++) {queue[tail++] = cur->children[j];}}(*returnColumnSizes)[level++] = cnt;}*returnSize = level;free(queue);return ans;
}