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

编程题 03-树2 List Leaves【PAT】

文章目录

  • 题目
    • 输入格式
    • 输出格式
    • 输入样例
    • 输出样例
  • 题解
    • 解题思路
    • 完整代码

编程练习题目集目录

题目

  Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

输入格式

  Each input file contains one test case. For each case, the first line gives a positive integer N ( ≤ 10 ) N (≤10) N(10) which is the total number of nodes in the tree − − -- and hence the nodes are numbered from 0 0 0 to N − 1 N−1 N1. Then N N N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a " − " "-" "" will be put at the position. Any pair of children are separated by a space.

输出格式

  For each test case, print in one line all the leaves’ indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

输入样例

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

输出样例

4 1 5

题解

解题思路

在这里插入图片描述
  找到树的根结点,根据树的根结点以及其它一系列结点构建树,按照从上到下,从左到右的顺序(层次遍历)输出这棵树的叶子结点。

完整代码

#include <iostream>         // 包含标准输入输出流库
#include <queue>            // 包含队列数据结构库using namespace std;        // 使用标准命名空间#define MaxN  10            // 定义最大节点数量// 定义二叉树节点结构
struct TreeNode
{int Data;           // 节点存储的数据int Left;           // 左子节点的索引,若无左子节点则为-2int Right;          // 右子节点的索引,若无右子节点则为-2
} TN[MaxN];             // TN[]为全局变量,存储所有节点信息,最多MaxN个节点int buildTree(TreeNode T[], int n);         // 构建二叉树,并返回根节点索引
void Traversal(int root);                   // 层序遍历二叉树,并输出叶节点值int main(void)
{int N, root;                            // N表示树的节点数量,root表示根节点索引cin >> N;root = buildTree(TN, N);                // 构建树并获取根节点索引Traversal(root);                        // 层序遍历并输出叶节点值return 0;
}// 构建二叉树
int buildTree(TreeNode T[], int n)
{int i, check[MaxN];                  // 检查数组,用于标记是否为子节点char left, right;                    // 用于存储左右子节点的输入字符if (n == 0) {                        // 如果节点数量为0,返回-2表示空树return -2;}for (i = 0; i < n; i++) {           // 初始化所有节点,初始时假设所有节点都不是子节点check[i] = -1;                  // -1表示非子节点TN[i].Data = i;                 // 每个节点的编号即为自己的数据}for (i = 0; i < n; i++) {           // 根据输入构建树cin >> left >> right;           // 输入当前节点的左右子节点信息if (left != '-') {                  // 如果有左子节点T[i].Left = left - '0';         // 将字符转换为索引check[T[i].Left] = 1;           // 标记左子节点对应的索引为子节点}else {T[i].Left = -2;                 // 表示无左子节点}if (right != '-') {                 // 如果有右子节点T[i].Right = right - '0';       // 将字符转换为索引check[T[i].Right] = 1;          // 标记右子节点对应的索引为子节点}else {T[i].Right = -2;                // 表示无右子节点}}// 查找根节点(未被标记为子节点的节点)for (i = 0; i < n; i++) {if (check[i] == -1) break;          // 找到根节点}return i;                               // 返回根节点索引
}
// 层序遍历二叉树并输出叶节点值
void Traversal(int root)
{queue<struct TreeNode> Q;               // 定义一个队列用于层序遍历struct TreeNode T;                      // 临时存储队列中的节点if (root == -2) {                       // 如果根节点不存在,直接返回return;}Q.push(TN[root]);                       // 将根节点入队int flag = 0;                           // 标志变量,用于控制输出格式while (!Q.empty()) {                    // 当队列非空时T = Q.front();                      // 取出队列头部节点Q.pop();                            // 出队if (T.Left == -2 && T.Right == -2) {        // 如果当前节点是叶节点if (flag == 1) cout << " ";             // 如果不是第一个叶节点,输出空格else flag = 1;                          // 标记已经输出过叶节点printf("%d", T.Data);             // 输出叶节点的值}if (T.Left != -2) {Q.push(TN[T.Left]);                     // 如果有左子节点,将左子节点入队}if (T.Right != -2) {                        // 如果有右子节点,将右子节点入队Q.push(TN[T.Right]);}}
}

相关文章:

  • 为什么要选择七彩喜数字康养平台?加盟后有何优势?
  • Oracle版本、补丁及升级(12)——补丁及补丁集
  • 2011-2020年各省粗离婚率数据
  • 解密企业级大模型智能体Agentic AI 关键技术:MCP、A2A、Reasoning LLMs- consistency is the key
  • AI时代的弯道超车之第十二章:英语和编程重要性?
  • 动态规划问题 -- 多状态模型(删除并获得点数)
  • MySQL之基础索引
  • 第二十九节:直方图处理-直方图均衡化
  • Made with Unity | 拓展“双点”宇宙版图
  • 【Python3教程】Python3基础篇之输入与输出
  • Redis学习打卡-Day1-SpringDataRedis、有状态无状态
  • hyper-v安装ubuntu后时磁盘空间扩容
  • 中国近代史1
  • Q1财报揭示:用户增长与客单价下跌对eBay卖家的蝴蝶效应
  • 网络层简单习题
  • 免费Ollama大模型集成系统——Golang
  • gmsh读取 STEP 文件并划分网格
  • 300. 最长递增子序列
  • Linux动态库与静态库
  • 编译支持CUDA-aware的OpenMPI
  • 获派驻6年后,中国驻厄瓜多尔大使陈国友即将离任
  • 张广智︱“编年事辑”:打开学人心路历程的窗户
  • 青海省交通运输厅副厅长田明有接受审查调查
  • 中科飞测将投资超10亿元,在上海张江成立第二总部
  • 【社论】公平有序竞争,外卖行业才能多赢
  • 科技部等七部门:优先支持取得关键核心技术突破的科技型企业上市融资