二叉树及其遍历

一遍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的更多相关文章

  1. 哈夫曼树(三)之 Java详解

    前面分别通过C和C++实现了哈夫曼树,本章给出哈夫曼树的java版本. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载请注明出处:htt ...

  2. 哈夫曼树(二)之 C++详解

    上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...

  3. 哈夫曼树(一)之 C语言详解

    本章介绍哈夫曼树.和以往一样,本文会先对哈夫曼树的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可.若 ...

  4. 哈夫曼树——c++

    哈夫曼树的介绍 Huffman Tree,中文名是哈夫曼树或霍夫曼树,它是最优二叉树. 定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若树的带权路径长度达到最小,则这棵树被称为哈夫曼树. 这个定 ...

  5. 哈夫曼(Huffman)树+哈夫曼编码

    前天acm实验课,老师教了几种排序,抓的一套题上有一个哈夫曼树的题,正好之前离散数学也讲过哈夫曼树,这里我就结合课本,整理一篇关于哈夫曼树的博客. 主要摘自https://www.cnblogs.co ...

  6. 哈夫曼树C++实现详解

    哈夫曼树的介绍 Huffman Tree,中文名是哈夫曼树或霍夫曼树,它是最优二叉树. 定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若树的带权路径长度达到最小,则这棵树被称为哈夫曼树. 这个定 ...

  7. 用WEKA进行数据挖掘

    学习于IBM教学文档 数据挖掘学习与weka使用 第二部 分分类和集群 分类 vs. 群集 vs. 最近邻 在我深入探讨每种方法的细节并通过 WEKA 使用它们之前,我想我们应该先理解每个模型 - 每 ...

  8. 数据结构与算法-Python/C(目录)

    第一篇 基本概念 01 什么是数据结构 02 什么是算法 03 应用实例-最大子列和问题 第二篇 线性结构 01 线性表及其实现 02 堆栈 03 队列 04 应用实例-多项式加法运算 05 小白专场 ...

  9. List Leaves 树的层序遍历

    3-树2 List Leaves (25 分) Given a tree, you are supposed to list all the leaves in the order of top do ...

  10. 树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】

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

随机推荐

  1. 安装和卸载windows程序

    安装windows service通常有两种工具 1.Framework目录下的installutil.exe工具.2.visual studio命令行工具 在这里我要说的是当我们使用的系统是64位的 ...

  2. SVN+FTP服务器搭建(一)——SVN安装配置篇

    Subversion是一个自由,开源的版本控制系统.在Subversion管理下,文件和目录可以超越时空.Subversion将文件存放在中心版本库里.这个版本库很像一个普通的文件服务器,不同的是,它 ...

  3. Android开发-API指南-<intent-filter>

    <intent-filter> 英文原文:http://developer.android.com/guide/topics/manifest/intent-filter-element. ...

  4. 学习练习 java20160507作业

    第一题 求水仙花的个数: //求水仙花数 int zongshu = 0; for(int i =100; i<=999;i++) { int bai = i/100; //求百位上面的数字 i ...

  5. centreon 画图x轴乱码

    rrdtool默认不指定locale,使用本地locale.乱码我估记是中文字体,由于操作系统最小化安装,本地没有中文字体,导致乱码. 1 安装中文字体 yum -y install wqy-zenh ...

  6. jQ复制按钮的插件zclip

    zclip官网:http://www.steamdev.com/zclip/ swf文件国内下载:ZeroClipboard.swf jQuery-zclip是一个复制内容到剪贴板的jQuery插件, ...

  7. druid parser

    有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 druid parser

  8. Oracle笔记 十二、PL/SQL 面向对象oop编程

    ------------------------抽象数据类型----------- --创建地址类型,一定要加as object,还可以在类型中加过程或方法 create or replace typ ...

  9. C#中string类型前加@标志的作用

    转自:http://stackoverflow.com/questions/4879152/c-sharp-before-a-string   (stackoverflow) string字符串前加@ ...

  10. ASP.NET中的Eval与DataBinder.Eval()方法

    1.bind是一种双向数据绑定,有数据源时才会有改变. 2..net1.x版本中有DataBinder(Container.DataItem,"数据项")  单向数据绑定 .net ...