1.题目要求:
给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
举例:
2.此题思路:使用层序遍历,把结点值存入二维数组中
3.做题步骤:
(1).先创建队列和出队函数,入队函数
//创建队列
typedef struct queue{
struct TreeNode* data;
struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
newnode->data = data;
newnode->next = NULL;
if(*head == NULL){
*head = newnode;
return;
}
queue_t* tail = *head;
while(tail->next != NULL){
tail = tail->next;
}
tail->next = newnode;
}
//出队
struct TreeNode* pop(queue_t** head){
struct TreeNode* x = (*head)->data;
(*head) = (*head)->next;
return x;
}
(2).再创两个数组,一个存入层序遍历的结点值,一个存入树中每行的结点数:
returnSize = 0;
//判断是否为空,如果为空,则返回空指针
if(root == NULL){
return NULL;
}
int count = 1;
int nextcount = 0;
int depth = 0;
queue_t* enquence = NULL;
int size = 0;
int* number = (int*)malloc(sizeof(int) * 2000);//设置层序遍历数组
int j = 0;
int* one_line_count = (int*)malloc(sizeof(int) * 2000);//存入每层的结点数
int j_1 = 0;
push(&enquence,root);
size++;
(3).然后开始层序遍历:
//开始层序遍历
while(size != 0){
depth++;
for(int i = 0;i < count;i++){
struct TreeNode* temp = pop(&enquence);
number[j] = temp->val;
j++;
size--;
if(temp->left != NULL){
push(&enquence,temp->left);
nextcount++;
size++;
}
if(temp->right != NULL){
push(&enquence,temp->right);
nextcount++;
size++;
}
}
one_line_count[j_1] = count;
j_1++;
count = nextcount;
nextcount = 0;;
}
(4).创建二维数组:
//创立二维数组
int** node = (int**)malloc(sizeof(int*) * j_1);
for(int i = 0;i < j_1;i++){
node[i] = (int*)malloc(sizeof(int) * one_line_count[i]);
}
(5).往里面存入值:
//往里面存入值
for(int i = 0;i < j_1;i++)
{
for(int j = 0;j < one_line_count[i];j++){
node[i][j] = number[f];
f++;
}
}
4.以下位全部代码:
/**
* 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().
*/
//创建队列
typedef struct queue{
struct TreeNode* data;
struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
newnode->data = data;
newnode->next = NULL;
if(*head == NULL){
*head = newnode;
return;
}
queue_t* tail = *head;
while(tail->next != NULL){
tail = tail->next;
}
tail->next = newnode;
}
//出队
struct TreeNode* pop(queue_t** head){
struct TreeNode* x = (*head)->data;
(*head) = (*head)->next;
return x;
}
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
*returnSize = 0;
//判断是否为空,如果为空,则返回空指针
if(root == NULL){
return NULL;
}
int count = 1;
int nextcount = 0;
int depth = 0;
queue_t* enquence = NULL;
int size = 0;
int* number = (int*)malloc(sizeof(int) * 2000);//设置层序遍历数组
int j = 0;
int* one_line_count = (int*)malloc(sizeof(int) * 2000);//存入每层的结点数
int j_1 = 0;
push(&enquence,root);
size++;
//开始层序遍历
while(size != 0){
depth++;
for(int i = 0;i < count;i++){
struct TreeNode* temp = pop(&enquence);
number[j] = temp->val;
j++;
size--;
if(temp->left != NULL){
push(&enquence,temp->left);
nextcount++;
size++;
}
if(temp->right != NULL){
push(&enquence,temp->right);
nextcount++;
size++;
}
}
one_line_count[j_1] = count;
j_1++;
count = nextcount;
nextcount = 0;;
}
//创立二维数组
int** node = (int**)malloc(sizeof(int*) * j_1);
for(int i = 0;i < j_1;i++){
node[i] = (int*)malloc(sizeof(int) * one_line_count[i]);
}
int f = 0;
//往里面存入值
for(int i = 0;i < j_1;i++)
{
for(int j = 0;j < one_line_count[i];j++){
node[i][j] = number[f];
f++;
}
}
*returnSize = j_1;
*returnColumnSizes = (int*)malloc((*returnSize) * sizeof(int));
for(int i = 0;i < (*returnSize);i++){
(*returnColumnSizes)[i] = one_line_count[i];
}
return node;
}
如果各位大佬觉得好的话,可以给个免费的赞吗?谢谢各位大佬了^ _ ^