二叉树及其遍历

一遍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. Storm实时计算框架的编程模式

    storm分布式流式计算框架. nimbus:主进程服务(职责就是任务的分配的,程序的分发) supervisor:工作进程服务(职责就是启动线程池,接受任务,运行任务,报告任务的运行状态) 注意容错 ...

  2. Oracle Grid Infrastructure: Understanding Split-Brain Node Eviction (文档 ID 1546004.1)

    In this Document   Purpose   Scope   Details   What does "split brain" mean?   Why is this ...

  3. [转]SVN-版本控制软件

    一.版本控制软件 1.为什么需要版本控制软件 问题:① 团队开发 ② 异地协作 ③ 版本回退 2.解决之道 SCM(Software Configuration Management):软件配置管理 ...

  4. [ CodeVS冲杯之路 ] P1092

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/1092/ 嗯,这道题有一定难度啊,需要先用扩展欧几里得算法求出逆元,然后按照大小构一颗带边权为小时数的树 树链剖分后在树 ...

  5. 使用ImageNet在faster-rcnn上训练自己的分类网络

    具体代码见https://github.com/zhiyishou/py-faster-rcnn 这是我对cup, glasses训练的识别 faster-rcnn在fast-rcnn的基础上加了rp ...

  6. [神器推荐]node-webkit:跨平台桌面web应用的神器,非常有用(转)

    11月8号在清华拍的银杏树 http://finalshares.com/read-931

  7. Laxcus大数据管理系统2.0(13)- 总结

    总结 以上从多个角度阐述了Laxcus主要组成部分和应用情况.所有设计都是基于现实环境下的评估.对比.测试和考量.设计的基本思路很明确,就是将各项功能分解.细化.归类,形成一个个可以独立.小的模块,每 ...

  8. struts2拦截器拦截成功后每次请求都出现拦截时的错误信息

    action中验证方法 在执行execute之前执行 @Override    public void validate() {        // TODO Auto-generated metho ...

  9. javascript设计模式-组合模式

    组合模式所要解决的问题: 可以使用简单的对象组合成复杂的对象,而这个复杂对象有可以组合成更大的对象.可以把简单这些对象定义成类,然后定义一些容器类来存储这些简单对象. 客户端代码必须区别对象简单对象和 ...

  10. 【drp 10】JSP页面中model1和model2的区别

    一.基本概念 1.1,model1 model1的开发模式是:jsp+javabean的模式,它的核心是JSP页面,在这个页面中,jsp页面负责整合页面和javabean(业务逻辑),而且渲染页面.它 ...