102. 二叉树的层序遍历
代码实现:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 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(). */ // returnColumnSizes:用于存储每个数组中元素个数(每行数量) int** levelOrder(struct TreeNode *root, int *returnSize, int **returnColumnSizes) { *returnSize = 0; int **res = (int**)malloc(sizeof(int*) * 2000); *returnColumnSizes = (int*)malloc(sizeof(int) * 2000); if (root == NULL) { return res; } struct TreeNode *queue[2001]; // 创建一个队列 注意:队头不放元素,长度须+1 int head = 0; // 队头 int tail = 0; // 队尾 queue[++tail] = root; // head:不存放数据,tail:存放数据 while (head < tail) { int colSize = 0, last = tail; res[*returnSize] = (int*)malloc(sizeof(int) * (last - head)); while (head < last) { struct TreeNode *node = queue[++head]; res[*returnSize][colSize++] = node->val; if (node->left != NULL) { queue[++tail] = node->left; } if (node->right != NULL) { queue[++tail] = node->right; } } (*returnColumnSizes)[*returnSize] = colSize; (*returnSize)++; } return res; }
107. 二叉树的层序遍历||
代码实现:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 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(). */ // returnColumnSizes:用于存储每个数组中元素个数 int** levelOrderBottom(struct TreeNode *root, int *returnSize, int **returnColumnSizes) { *returnSize = 0; int** res = (int**)malloc(sizeof(int*) * 2000); *returnColumnSizes = (int*)malloc(sizeof(int) * 2000); if (root == NULL) { return res; } struct TreeNode* queue[2001]; // 创建一个队列 注意:队头不放元素,长度须+1 int head = 0; // 队头 int tail = 0; // 队尾 queue[++tail] = root; // head:不存放数据,tail:存放数据 while (head < tail) { int colSize = 0, last = tail; res[*returnSize] = (int*)malloc(sizeof(int) * (last - head)); while (head < last) { struct TreeNode* node = queue[++head]; res[*returnSize][colSize++] = node->val; if (node->left) { queue[++tail] = node->left; } if (node->right) { queue[++tail] = node->right; } } (*returnColumnSizes)[*returnSize] = colSize; (*returnSize)++; } // 反转res数组 int **ret = (int**)malloc(sizeof(int*) * (*returnSize)); // 动态开辟一个二维数组ret int nums[*returnSize]; // 保存ret每行的大小(数组元素个数) for (int i = 0; i < *returnSize; i++) { nums[i] = (*returnColumnSizes)[i]; } for (int i = 0; i < *returnSize; i++) { (*returnColumnSizes)[i] = nums[*returnSize - i - 1]; } for (int i = 0; i < *returnSize; i++) { ret[i] = (int*)malloc(sizeof(int) * (*returnColumnSizes)[i]); for (int j = 0; j < nums[*returnSize - i - 1]; j++) { ret[i][j] = res[*returnSize - i - 1][j]; } } free(res); return ret; }
199. 二叉树的右视图
代码实现:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * Note: The returned array must be malloced, assume caller calls free(). */ int* rightSideView(struct TreeNode *root, int *returnSize) { int *ret = malloc(sizeof(int) * 100); memset(ret, 0, sizeof(int) * 100); *returnSize = 0; if (root == NULL) { return ret; } struct TreeNode *queue[101]; // 创建一个队列 int head = 0; // 队头 int tail = 0; // 队尾 queue[++tail] = root; // head:不存放数据,tail:存放数据 while (head < tail) { int last = tail; while (head < last) { struct TreeNode *node = queue[++head]; if (head == last) { // 保存每层最后一个结点 ret[(*returnSize)++] = node->val; } if (node->left) { queue[++tail] = node->left; } if (node->right) { queue[++tail] = node->right; } } } return ret; }
637. 二叉树的层平均值
代码实现:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * Note: The returned array must be malloced, assume caller calls free(). */ double* averageOfLevels(struct TreeNode *root, int *returnSize) { double *ret = malloc(sizeof(double) * 10000); memset(ret, 0, sizeof(int) * 10000); *returnSize = 0; if (root == NULL) { return ret; } struct TreeNode *queue[10001]; // 创建一个队列 int head = 0; // 队头 int tail = 0; // 队尾 queue[++tail] = root; // head:不存放数据,tail:存放数据 while (head < tail) { int last = tail; double sum = 0; int size = tail - head; // 每层结点个数 while (head < last) { struct TreeNode* node = queue[++head]; sum += node->val; if (node->left) { queue[++tail] = node->left; } if (node->right) { queue[++tail] = node->right; } } ret[(*returnSize)++] = sum / size; } return ret; }
429. N叉树的层序遍历
代码实现:
/** * 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(). */ int** levelOrder(struct Node *root, int *returnSize, int **returnColumnSizes) { int **res = malloc(sizeof(int*) * 1000); *returnSize = 0; *returnColumnSizes = (int*)malloc(sizeof(int) * 10000); if (root == NULL) { return res; } struct Node *queue[10001]; // 创建一个队列 注意:队头不放元素,长度须+1 int head = 0; // 队头 int tail = 0; // 队尾 queue[++tail] = root; // head:不存放数据,tail:存放数据 while (head < tail) { int colSize = 0, last = tail; res[*returnSize] = (int*)malloc(sizeof(int) * (last - head)); while (head < last) { struct Node *node = queue[++head]; res[*returnSize][colSize++] = node->val; for (int i = 0; i < node->numChildren; i++) { queue[++tail] = node->children[i]; } } (*returnColumnSizes)[*returnSize] = colSize; (*returnSize)++; } return res; }