03-树2 List Leaves (25分)

2024-03-06 09:38
文章标签 25 03 list leaves

本文主要是介绍03-树2 List Leaves (25分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

03-树2 List Leaves   (25分)

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

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer NN (1010) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N1N1. Then NN 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.

Output Specification:

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.

Sample Input:

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

Sample Output:

4 1 5

主要思路:
1、此题不允许调用STL的队列,需要自己构造队列,循环队列和链式队列都可以,我选择的循环队列
2、层次遍历二叉树

#include <iostream>
#include<vector>using namespace std;#define Max_Node 11
#define Last -1typedef struct node
{int left;int right;
}Node;int CreateTree(int N,vector<Node>& Tree)//建树并返回根
{char left;char right;for (int i=0; i<N; ++i){cin>>left>>right;if (left=='-'){Tree[i].left=Last;}else{Tree[i].left=left-'0';}if (right=='-'){Tree[i].right=Last;}else{Tree[i].right=right-'0';}}int flag[N];for (int i=0; i<N; ++i){flag[i]=0;}for (int i=0; i<N; ++i){if (Tree[i].left!=Last){flag[Tree[i].left]=1;}if (Tree[i].right!=Last){flag[Tree[i].right]=1;}}int sequence;for (sequence=0; sequence<N; ++sequence){if (flag[sequence]==0){break;}}return sequence;
}typedef struct queue//静态队列
{int Data[Max_Node];int Front;int Rear;int num;
}Queue;void Initialize_Queue(Queue* Q)
{for (int i=0; i<Max_Node; ++i){Q->Data[i]=0;}Q->Front=0;Q->Rear=0;Q->num=0;
}bool Push_Queue(int data,Queue* Q)
{if (Q->num==Max_Node){return false;}Q->Data[Q->Rear]=data;(Q->num)++;Q->Rear=(Q->Rear+1)%Max_Node;return true;
}int Pop_Queue(Queue* Q)
{int data;if (Q->num>0){data=Q->Data[Q->Front];}else{return -1;}(Q->num)--;Q->Front=(Q->Front+1)%Max_Node;return data;
}int main()
{vector<Node> Tree(Max_Node);int N=0;cin>>N;int Root=CreateTree(N,Tree);//ProcessQueue Q;Initialize_Queue(&Q);int flag=0;Push_Queue(Root, &Q);int value,left,right;while (Q.num>0){value=Pop_Queue(&Q);left=Tree[value].left;right=Tree[value].right;if (left!=Last){Push_Queue(left,&Q);}if (right!=Last){Push_Queue(right, &Q);}if (left==Last && right==Last){if (flag==0){flag=1;cout<<value;}else{cout<<' '<<value;}}}return 0;
}

这篇关于03-树2 List Leaves (25分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779612

相关文章

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

FreeRTOS内部机制学习03(事件组内部机制)

文章目录 事件组使用的场景事件组的核心以及Set事件API做的事情事件组的特殊之处事件组为什么不关闭中断xEventGroupSetBitsFromISR内部是怎么做的? 事件组使用的场景 学校组织秋游,组长在等待: 张三:我到了 李四:我到了 王五:我到了 组长说:好,大家都到齐了,出发! 秋游回来第二天就要提交一篇心得报告,组长在焦急等待:张三、李四、王五谁先写好就交谁的

【Python报错已解决】AttributeError: ‘list‘ object has no attribute ‘text‘

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 文章目录 前言一、问题描述1.1 报错示例1.2 报错分析1.3 解决思路 二、解决方法2.1 方法一:检查属性名2.2 步骤二:访问列表元素的属性 三、其他解决方法四、总结 前言 在Python编程中,属性错误(At

【JavaScript】LeetCode:21-25

文章目录 21 最大子数组和22 合并区间23 轮转数组24 除自身以外数组的乘积25 缺失的第一个正数 21 最大子数组和 贪心 / 动态规划贪心:连续和(count)< 0时,放弃当前起点的连续和,将下一个数作为新起点,这里提供使用贪心算法解决本题的代码。动态规划:dp[i]:以nums[i]为结尾的最长连续子序列(子数组)和。 dp[i] = max(dp[i - 1]

Vue day-03

目录 Vue常用特性 一.响应更新 1. 1 v-for更新监测 1.2 v-for就地更新 1.3 什么是虚拟DOM 1.4 diff算法更新虚拟DOM 总结:key值的作用和注意点: 二.过滤器 2.1 vue过滤器-定义使用 2.2 vue过滤器-传参和多过滤器 三. 计算属性(computed) 3.1 计算属性-定义使用 3.2 计算属性-缓存 3.3 计算属