把二叉树按层序输出c
时间: 2024-01-26 11:06:10 浏览: 29
以下是二叉树按层序输出的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点的结构体
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
// 定义队列节点的结构体
struct QueueNode {
struct TreeNode *data;
struct QueueNode *next;
};
// 定义队列结构体
struct Queue {
struct QueueNode *front;
struct QueueNode *rear;
};
// 初始化队列
void initQueue(struct Queue *q) {
q->front = q->rear = NULL;
}
// 队列是否为空
int isQueueEmpty(struct Queue *q) {
return q->front == NULL;
}
// 入队
void enqueue(struct Queue *q, struct TreeNode *data) {
struct QueueNode *newNode = (struct QueueNode *)malloc(sizeof(struct QueueNode));
newNode->data = data;
newNode->next = NULL;
if (isQueueEmpty(q)) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
}
// 出队
struct TreeNode *dequeue(struct Queue *q) {
if (isQueueEmpty(q)) {
return NULL;
}
struct QueueNode *temp = q->front;
struct TreeNode *data = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
return data;
}
// 层序遍历
void levelOrder(struct TreeNode *root) {
if (root == NULL) {
return;
}
struct Queue q;
initQueue(&q);
enqueue(&q, root);
while (!isQueueEmpty(&q)) {
int size = q.rear - q.front + 1;
for (int i = 0; i < size; i++) {
struct TreeNode *node = dequeue(&q);
printf("%d ", node->val);
if (node->left != NULL) {
enqueue(&q, node->left);
}
if (node->right != NULL) {
enqueue(&q, node->right);
}
}
}
}
// 创建二叉树节点
struct TreeNode *createNode(int val) {
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode));
newNode->val = val;
newNode->left = newNode->right = NULL;
return newNode;
}
int main() {
// 创建示例二叉树
struct TreeNode *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
// 输出层序遍历结果
levelOrder(root);
return 0;
}
```
以上是一个基于队列实现的二叉树按层序输出的C语言代码,你可以在本地编译运行查看结果。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)