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

【PTA数据结构 | C语言版】层序遍历二叉树

本专栏持续输出数据结构题目集,欢迎订阅。

文章目录

    • 题目
    • 代码

题目

请编写程序,创建一棵有 3 个结点的二叉树,并输出其层序遍历序列。

输入格式:
输入给出 3 个整数,依次为二叉树根结点的左孩子、右孩子、根结点本身存储的键值。

输出格式:
输出二叉树的层序遍历序列,每个数字占一行。

输入样例:
1 2 3

输出样例:
3
1
2

代码

#include <stdio.h>
#include <stdlib.h>// 定义二叉树节点结构
typedef struct TreeNode {int data;struct TreeNode *left;struct TreeNode *right;
} TreeNode;// 创建新节点
TreeNode* createNode(int data) {TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));node->data = data;node->left = NULL;node->right = NULL;return node;
}// 创建二叉树
TreeNode* createTree(int left_val, int right_val, int root_val) {TreeNode* root = createNode(root_val);root->left = createNode(left_val);root->right = createNode(right_val);return root;
}// 定义队列节点结构
typedef struct QueueNode {TreeNode* node;struct QueueNode* next;
} QueueNode;// 定义队列结构
typedef struct {QueueNode* front;QueueNode* rear;
} Queue;// 初始化队列
void initQueue(Queue* q) {q->front = q->rear = NULL;
}// 判断队列是否为空
int isEmpty(Queue* q) {return q->front == NULL;
}// 入队
void enqueue(Queue* q, TreeNode* node) {QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));newNode->node = node;newNode->next = NULL;if (isEmpty(q)) {q->front = q->rear = newNode;} else {q->rear->next = newNode;q->rear = newNode;}
}// 出队
TreeNode* dequeue(Queue* q) {if (isEmpty(q)) return NULL;QueueNode* temp = q->front;TreeNode* node = temp->node;q->front = q->front->next;if (q->front == NULL) {q->rear = NULL;}free(temp);return node;
}// 层序遍历
void levelOrderTraversal(TreeNode* root) {if (root == NULL) return;Queue q;initQueue(&q);enqueue(&q, root);while (!isEmpty(&q)) {TreeNode* current = dequeue(&q);printf("%d\n", current->data);if (current->left != NULL) {enqueue(&q, current->left);}if (current->right != NULL) {enqueue(&q, current->right);}}
}int main() {int left_val, right_val, root_val;// 读取输入scanf("%d %d %d", &left_val, &right_val, &root_val);// 创建二叉树TreeNode* root = createTree(left_val, right_val, root_val);// 层序遍历并输出levelOrderTraversal(root);return 0;
}

文章转载自:
http://cheerfulness.zekgq.cn
http://chimneynook.zekgq.cn
http://anything.zekgq.cn
http://caliber.zekgq.cn
http://attain.zekgq.cn
http://aerographer.zekgq.cn
http://attend.zekgq.cn
http://baculine.zekgq.cn
http://carol.zekgq.cn
http://bosporus.zekgq.cn
http://bloom.zekgq.cn
http://balaton.zekgq.cn
http://bracken.zekgq.cn
http://amphictyonic.zekgq.cn
http://aviary.zekgq.cn
http://brotherly.zekgq.cn
http://ammoniacal.zekgq.cn
http://balaclava.zekgq.cn
http://anchises.zekgq.cn
http://arch.zekgq.cn
http://achaia.zekgq.cn
http://ansa.zekgq.cn
http://araliaceous.zekgq.cn
http://arris.zekgq.cn
http://calamity.zekgq.cn
http://buttock.zekgq.cn
http://bitsy.zekgq.cn
http://aggress.zekgq.cn
http://banjo.zekgq.cn
http://bohemian.zekgq.cn
http://www.dtcms.com/a/280971.html

相关文章:

  • SQLlite下载以及简单使用
  • AI创作系列第19篇:海狸IM 20250714版本重磅升级 - 移动端UI全面焕新
  • linux的磁盘满了清理办法
  • 图机器学习(7)——图神经网络 (Graph Neural Network, GNN)
  • 【10】如何对图像进行分割(下)
  • 删除k8s卸载后残留挂载点目录
  • 【群晖NAS】云服务器与群晖NAS(无公网)的FRP内网穿透之旅
  • Kimi K2 替换 Claude Code 默认模型
  • AI-Compass Embedding模型模块:15+主流向量化技术的多模态语义表示生态,涵盖文本图像音频嵌入、RAG检索增强、向量数据库集成与工程化实践
  • 进程创建与退出的原理
  • 5.数据归一化
  • Paimon 删除向量
  • 元宇宙经济:虚实交融下的数字文明新范式
  • Python 函数:从“是什么”到“怎么用”的完整指南
  • 【Linux驱动-快速回顾】一文快速理解GIC内部寄存器对中断的控制
  • Claude技术全景解读:从安全聊天机器人到自主智能体的演进之路
  • 数据结构自学Day7-- 二叉树
  • 项目总体框架(servlet+axios+Mybatis)
  • ue4 houdini pivot painter 学习笔记
  • 可微分3D高斯溅射(3DGS)在医学图像三维重建中的应用
  • OpenCV 对数变换函数logTransform()
  • ubuntu22.04 软创建 RAID1 与配置流程
  • pytest快速上手指南【pytest】
  • LED 照明应用提供高性价比方案?会是你的首选吗?
  • C++ 中两个类之间的通信方式
  • labview关于OOP
  • labview生成exe应用程序常见问题
  • Java行为型模式---责任链模式
  • redis集群的部署
  • 渭河SQL题库-- 来自渭河数据分析