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

龙岗地区做网站公司在线生成html网页

龙岗地区做网站公司,在线生成html网页,济南网站建设的方案,有专业做网站的吗文章目录 题目输入格式输出格式输入样例输出样例 题解解题思路完整代码 编程练习题目集目录 题目 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, …

文章目录

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

编程练习题目集目录

题目

  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]);}}
}
http://www.dtcms.com/wzjs/523007.html

相关文章:

  • 做公务员考试哪个网站好优化营商环境建议
  • 大型网站系统百度一下你就知道了百度
  • 代理网页是干什么的西青seo
  • 网站营销单页怎么设计方案百度电话怎么转人工
  • 网络规划工程师seo优化个人博客
  • 数字营销技术应用网站什么优化
  • 企业网站seo点击软件网站定制的公司
  • 医疗网站备案免费域名解析平台
  • 做个产品网站要多少钱推广赚钱的微信小程序
  • 南宁住房和城乡建设委员会网站搜索引擎调价平台哪个好
  • 做宣传 为什么要做网站那优化网站关键词的技巧
  • 做网站需要购买什么seo交互论坛
  • 网站制作说明书永久免费的建站系统有哪些
  • 苏州相城网站建设搜索优化软件
  • wordpress 登录页面变了高平网站优化公司
  • 企业网站源代码免费下载农产品营销方案
  • 淘宝网站优化实例网络营销推广主要做什么
  • 长沙交互网站设计服务商关键词挖掘长尾词
  • 网站建立不安全怎么取消友情链接教程
  • 零基础学前端要多久才能成手优化疫情防控 这些措施你应该知道
  • 公司网站在百度搜不到上海抖音seo
  • 孝感网站建设孝感seo优化的网站
  • 湖北建设网站首页国内销售平台有哪些
  • 国外自助建站免费建站平台市场推广方案和思路
  • 在哪里可以学做饰品网站河南专业网站建设
  • 钓鱼网站在线下载外贸网络推广经验
  • struts2 做的网站看书网站排名
  • 网站建设 服务内容免费发帖论坛大全
  • 菏泽官方网站seo外链
  • 网络公司的手机网站企业培训十大热门课程