二叉树及其遍历

一遍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. UVA12653 Buses

    Problem HBusesFile: buses.[c|cpp|java]Programming competitions usually require infrastructure and or ...

  2. Grunt 之 使用 JavaScript 语法检查工具 jshint

    前端开发环境准备好了,我们准备开始进行开发. 前端开发的主力语言是 JavaScript,这是一种脚本语言,没有编译器,也就没有了编译器带给我们的语法检查,怎样保证代码的质量呢?jshint 是一个强 ...

  3. opencv基于HSV的肤色分割

    //函数功能:在HSV颜色空间对图像进行肤色模型分割 //输入:src-待处理的图像,imgout-输出图像 //返回值:返回一个iplimgae指针,指向处理后的结果 IplImage* SkinS ...

  4. flex安装时停在计算时间界面的解决办法

    现象:安装FLEX BUILDER4.6时停在计算时间界面,过了一会后弹出安装失败的对话框. 环境:WIN7 解决: 1.下载AdobeCreativeCloudCleanerTool, 地址:htt ...

  5. Java之注解

    package com.demo.test; import java.lang.annotation.Documented; import java.lang.annotation.ElementTy ...

  6. 二模08day2解题报告

    T1.引爆炸弹(bomb) N个炸弹构成一棵树,引爆一颗叶节点,会一直引爆到根节点.每颗炸弹有一个价值,求引爆k个炸弹的最大价值. 既然是一棵树,那么自然想到dp.所以先树形dp了一遍(由于可能出现多 ...

  7. Leetcode028. Implement strStr()

    class Solution { public: int strStr(string haystack, string needle) { ; //needle empty ; //haystack ...

  8. Landsat 8 OLI_TIRS 卫星数字产品

      产品描述           2013 年2月11日,美国航空航天局(NASA) 成功发射Landsat-8卫星.Landsat-8卫星上携带两个传感器,分别是OLI陆地成像仪(Operation ...

  9. js获得浏览器的尺寸

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. windows live writer向cnblog发布文章设置(转)

    Windows Live Writer是非常不错的一个日志发布工具,支持本地写文章,非常方便将word 中内容,包括图片进行处理,有点感觉相见恨晚. Live Writer最新版本下载地址是什么? h ...