当前位置: 首页 > news >正文

leetcode 107.二叉树的层序遍||

1.题目要求:

给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

在这里插入图片描述
2.此题步骤:
1.先创建好队列,出队和入队函数:

//创建队列
typedef struct queue{struct TreeNode* value;struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));newnode->value = 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)->value;(*head) = (*head)->next;return x;
}

2.我们还要创造逆置函数:

void reverse(int* number,int rows){int left = 0;int right = rows - 1;while(left <= right){int temp = number[left];number[left] = number[right];number[right] = temp;left++;right--;}
}

3.在层序遍历之前,设置好各种变量(详情已在代码块里):

*returnSize = 0;if(root == NULL){return NULL;}int* each_line_nodes = (int*)malloc(sizeof(int)*2000);//记录每行结点数int j_1 = 0;int* level_order_number = (int*)malloc(sizeof(int)* 2000);//层序遍历的数组int j_2 = 0;int depth = 0;//树的高度int count = 1;//根结点的个数int nextcount = 0;//下一个结点的个数int size = 0;//记录队列中的个数queue_t* quence = NULL;//设置队列

4.进行层序遍历:

 //进行层序遍历push(&quence,root);size++;while(size != 0){depth++;for(int i = 0;i < count;i++){struct TreeNode* temp = pop(&quence);size--;level_order_number[j_2] = temp->val;j_2++;if(temp->left != NULL){push(&quence,temp->left);size++;nextcount++;}if(temp->right != NULL){push(&quence,temp->right);size++;nextcount++;}}each_line_nodes[j_1] = count;j_1++;count = nextcount;nextcount = 0;}

5.创建二维数组,把层序遍历的数组倒着存到二维数组中:

//设立二维数组int** array = (int**)malloc(sizeof(int*)* depth);//设置函数,逆置每行结点数reverse(each_line_nodes,j_1);for(int i = 0;i < depth;i++){array[i] = (int*)malloc(sizeof(int) * each_line_nodes[i]);}int f = j_2 - 1;//把层序遍历的数组倒着存入二维数组中for(int i = 0;i < depth;i++){for(int j = 0;j < each_line_nodes[i];j++){array[i][j] = level_order_number[f];f--;}}

6.开始逆置二维数组每行的元素,然后返回二维数组的地址:

 //颠倒每个二维数组的元素for(int i = 0;i < depth;i++){reverse(array[i],each_line_nodes[i]);}*returnSize = depth;*returnColumnSizes = (int*)malloc(sizeof(int) * (*returnSize));for(int i = 0;i < depth;i++){(*returnColumnSizes)[i] = each_line_nodes[i];}return array;

以下为我的全部代码:

/*** 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* value;struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));newnode->value = 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)->value;(*head) = (*head)->next;return x;
}
void reverse(int* number,int rows){int left = 0;int right = rows - 1;while(left <= right){int temp = number[left];number[left] = number[right];number[right] = temp;left++;right--;}
}
int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {*returnSize = 0;if(root == NULL){return NULL;}int* each_line_nodes = (int*)malloc(sizeof(int)*2000);//记录每行结点数int j_1 = 0;int* level_order_number = (int*)malloc(sizeof(int)* 2000);//层序遍历的数组int j_2 = 0;int depth = 0;//树的高度int count = 1;//根结点的个数int nextcount = 0;//下一个结点的个数int size = 0;//记录队列中的个数queue_t* quence = NULL;//设置队列//进行层序遍历push(&quence,root);size++;while(size != 0){depth++;for(int i = 0;i < count;i++){struct TreeNode* temp = pop(&quence);size--;level_order_number[j_2] = temp->val;j_2++;if(temp->left != NULL){push(&quence,temp->left);size++;nextcount++;}if(temp->right != NULL){push(&quence,temp->right);size++;nextcount++;}}each_line_nodes[j_1] = count;j_1++;count = nextcount;nextcount = 0;}//设立二维数组int** array = (int**)malloc(sizeof(int*)* depth);//设置函数,逆置每行结点数reverse(each_line_nodes,j_1);for(int i = 0;i < depth;i++){array[i] = (int*)malloc(sizeof(int) * each_line_nodes[i]);}int f = j_2 - 1;//把层序遍历的数组倒着存入二维数组中for(int i = 0;i < depth;i++){for(int j = 0;j < each_line_nodes[i];j++){array[i][j] = level_order_number[f];f--;}}//颠倒每个二维数组的元素for(int i = 0;i < depth;i++){reverse(array[i],each_line_nodes[i]);}*returnSize = depth;*returnColumnSizes = (int*)malloc(sizeof(int) * (*returnSize));for(int i = 0;i < depth;i++){(*returnColumnSizes)[i] = each_line_nodes[i];}return array;
}

在这里插入图片描述
好了,这就是我的代码,如果各位觉得可以的话,可以给个免费的赞吗?谢谢了^ _ ^ .

http://www.lryc.cn/news/415114.html

相关文章:

  • C++在网络安全领域的应用
  • Chapter 26 Python魔术方法
  • 基于Transformer的语音识别与音频分类
  • leetcode数论(1362. 最接近的因数)
  • sqli-labs-master less1-less6
  • 力扣287【寻找重复数】
  • 【2024蓝桥杯/C++/B组/传送阵】
  • (四十一)大数据实战——spark的yarn模式生产环境部署
  • 【深度学习实战(53)】classification_report()
  • 计算机网络基础之网络套接字socket编程(初步认识UDP、TCP协议)
  • 手撕Python!模块、包、库,傻傻分不清?一分钟带你弄明白!
  • Linux--序列化与反序列化
  • 使用C#和 aspose.total 实现替换pdf中的文字(外语:捷克语言的pdf),并生成新的pdf导出到指定路径
  • 【Material-UI】Autocomplete中的高亮功能(Highlights)详解
  • Android 11(R)启动流程 初版
  • 从零安装pytorch
  • 2024.07.28 校招 实习 内推 面经
  • python实现小游戏——植物大战僵尸(魔改版本)
  • 基于K210智能人脸识别+车牌识别系统(完整工程资料源码)
  • 8.怎么配嵌套子路由,以及它的作用
  • 【海贼王航海日志:前端技术探索】HTML你学会了吗?(二)
  • 体系结构论文导读(三十一)(下):Soft errors in DNN accelerators: A comprehensive review
  • Python在指定文件夹下创建虚拟环境
  • 【SpringBoot】 定时任务之任务执行和调度及使用指南
  • 理解 Objective-C 中 +load 方法的执行顺序
  • 切面条问题算法的详解
  • JNDI注入
  • SQL Server数据库文件过大而无法直接导出解决方案
  • 学习日志8.4--DHCP攻击防范
  • 解决多个Jenkins Master实例共享Jenkins_home目录的问题(加锁解锁机制)