03-树2 List Leaves
二叉树及其遍历
一遍AC,挺开心的hhh~
简单讲下思路:叶子,顾名思义就是没有左右子树的结点。由于题目要求,叶子结点的输出顺序是从上往下,从左往右。所以用层序遍历法。
当然,这里先找到root树的根。运用队列,将root进队列。然后依次将队头出队,若是叶子则输出,否则且将其有的左右孩子进队,达到层序遍历,就是从上往下,从左往右的要求。
当队列为空,即遍历整个树后,结束。
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 N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then 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.
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
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
#define OK 1
#define ERROR 0 #define MaxTree 10
#define Null -1 //区别于系统的NULL 0 typedef int Status;/* Status是函数的类型,其值是函数结果状态代码,如OK等 */ typedef struct TreeNode
{
int left;
int right;
} Tree; Tree T[MaxTree]; int BulidTree(Tree T[])
{
int N, check[MaxTree], root = Null; //root = Null 空树则返回Null
char cl, cr; //左右孩子序号
scanf("%d\n",&N);
if(N) {
for(int i = ; i < N; i++)
check[i] = ;
for(int i = ; i < N; i++) {
scanf("%c %c\n",&cl,&cr);
//找root
if(cl != '-') {
T[i].left = cl - '';
check[T[i].left] = ; //不是根节点
}else {
T[i].left = Null;
}
if(cr != '-') {
T[i].right = cr - '';
check[T[i].right] = ; //不是根节点
}else {
T[i].right = Null;
}
} for(int i = ; i < N; i++) //check[]=0的为根节点
if(!check[i]) {
root = i;
break;
}
}
return root;
} void isLeaves(int root)
{
queue<int> queue;
queue.push(root);
int flagSpace = ;
while(!queue.empty()) {
int temp = queue.front();
queue.pop(); //pop()虽然会移除下一个元素,但是并不返回
if( (T[temp].left == Null) && (T[temp].right == Null) ) { //该结点是叶子
if(flagSpace) { //控制输出格式
printf("%d", temp);
flagSpace = ;
}else {
printf(" %d", temp);
}
}else {
if(T[temp].left != Null) //不是叶子把它儿子push进队列
queue.push(T[temp].left);
if(T[temp].right != Null)
queue.push(T[temp].right);
}
}
printf("\n");
}
int main()
{
int root1;
root1 = BulidTree(T);
isLeaves(root1);
return ;
}
03-树2 List Leaves的更多相关文章
- 哈夫曼树(三)之 Java详解
前面分别通过C和C++实现了哈夫曼树,本章给出哈夫曼树的java版本. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载请注明出处:htt ...
- 哈夫曼树(二)之 C++详解
上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...
- 哈夫曼树(一)之 C语言详解
本章介绍哈夫曼树.和以往一样,本文会先对哈夫曼树的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可.若 ...
- 哈夫曼树——c++
哈夫曼树的介绍 Huffman Tree,中文名是哈夫曼树或霍夫曼树,它是最优二叉树. 定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若树的带权路径长度达到最小,则这棵树被称为哈夫曼树. 这个定 ...
- 哈夫曼(Huffman)树+哈夫曼编码
前天acm实验课,老师教了几种排序,抓的一套题上有一个哈夫曼树的题,正好之前离散数学也讲过哈夫曼树,这里我就结合课本,整理一篇关于哈夫曼树的博客. 主要摘自https://www.cnblogs.co ...
- 哈夫曼树C++实现详解
哈夫曼树的介绍 Huffman Tree,中文名是哈夫曼树或霍夫曼树,它是最优二叉树. 定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若树的带权路径长度达到最小,则这棵树被称为哈夫曼树. 这个定 ...
- 用WEKA进行数据挖掘
学习于IBM教学文档 数据挖掘学习与weka使用 第二部 分分类和集群 分类 vs. 群集 vs. 最近邻 在我深入探讨每种方法的细节并通过 WEKA 使用它们之前,我想我们应该先理解每个模型 - 每 ...
- 数据结构与算法-Python/C(目录)
第一篇 基本概念 01 什么是数据结构 02 什么是算法 03 应用实例-最大子列和问题 第二篇 线性结构 01 线性表及其实现 02 堆栈 03 队列 04 应用实例-多项式加法运算 05 小白专场 ...
- List Leaves 树的层序遍历
3-树2 List Leaves (25 分) Given a tree, you are supposed to list all the leaves in the order of top do ...
- 树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
随机推荐
- [转]SVN-版本控制软件
一.版本控制软件 1.为什么需要版本控制软件 问题:① 团队开发 ② 异地协作 ③ 版本回退 2.解决之道 SCM(Software Configuration Management):软件配置管理 ...
- svn老鸟转用git必须理解的概念
不都是SCM代码管理嘛,有很大区别么?很多svn老鸟都是抱着这样的心态去学习git,然后无一幸免地陷入“查阅过很多资料,依然掌握不好”的困境,至少我们团队是这样的. 网上的资料确实已经很多了,却没有把 ...
- DoTween使用
官网:http://dotween.demigiant.com/ 1.step 这里使用lamda表达式,通过dotween的to方法将其移动到 Vector3(348, 196, 0)的值返回到Ve ...
- asp.net中如何绑定combox下拉框数据(调用存储过程)
#region 绑定类型(商品类型.仓库名称) public void DataType_Bind(int _peoid) { DataTable dt_goodsname = new DataTab ...
- PL/SQL 导入excel表格到oracle数据表
通过使用PL/SQL 批量查询取数时,将excel中的每一列数据复制黏贴进新建的中间表,黏贴时会有贴歪的情况,也就是某些列会从第二第三行开始插入整列,导致数据乱掉,然后好像又不支持批量删除整列数据,所 ...
- random类的使用
小栗子a如下: string[] punch = new[] { "石头", "剪刀", "布" }; string myPunch; pu ...
- java web学习
一直想下决心好好学下java web,但是总是间断,虽然我的方向是ios,但是觉得后台也是相当重要,毕竟移动端实际上更多也就是展示后台拉取到的信息,搞移动端的不能总是受制于后台,各位看官觉得呢? 这两 ...
- NHibernate输出SQL语句
用了NHierbate之后,很少需要写原生的SQL语句,由于总是看不到SQL语句,所以有时候对SQL调优非常不利.因此产生了让NHibernate输出它所生成的SQL语句的想法,以便于后续调优. 一. ...
- IEF could not decode Chinese character in IE history well
My friend is working on some case, and she looks not in the mood. I ask her what's going on. She wan ...
- How to search compound files
Last week my friend told me that she made a terrible mistake. She conducted raw serch and found no s ...