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

相关文章

java streamfilter list 过滤的实现

《javastreamfilterlist过滤的实现》JavaStreamAPI中的filter方法是过滤List集合中元素的一个强大工具,可以轻松地根据自定义条件筛选出符合要求的元素,本文就来... 目录1. 创建一个示例List2. 使用Stream的filter方法进行过滤3. 自定义过滤条件1. 定

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

java两个List的交集,并集方式

《java两个List的交集,并集方式》文章主要介绍了Java中两个List的交集和并集的处理方法,推荐使用Apache的CollectionUtils工具类,因为它简单且不会改变原有集合,同时,文章... 目录Java两个List的交集,并集方法一方法二方法三总结java两个List的交集,并集方法一

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

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