3-树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 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
 
思路: 首先确定程序框架:  主体程序:建立一个二叉树————二叉树的遍历 ———— 输出叶节点
建立二叉树的过程,先定义一个struct 结构。scanf 获得左子树和右子树。
二叉树的遍历是通过构造一个队列,如果是叶节点,则没有左子树和右子树,则输出该节点。
同时注意格式:中间用空格隔开,因此使用一个int leaves记录叶节点的个数,第一个叶节点不输出空格。
 
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define Tree int
#define Null -1
queue<Tree> q;
struct TreeNode{
int data;
Tree left;
Tree right;
}T[]; Tree BuildTree(struct TreeNode T[]){ //建树过程
int n,i;
scanf("%d",&n);
getchar(); //接收空格
int check[n]={};
char cl,cr;
if(!n)return Null;
if(n){
for(i=;i<n;i++){
scanf("%c %c",&cl,&cr);
T[i].data=i;
getchar();
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(i=;i<n;i++){
if(!check[i])return i;
}
}
void ListTraverse(Tree root){ //树的层序遍历,通过队列实现
int leaves=; //用于记录叶节点
q.push(root);
while(!q.empty()){
Tree node=q.front();
q.pop();
if(T[node].left==Null&&T[node].right==Null){
if(leaves++)printf(" "); //第一个叶节点前面不加空格
printf("%d",T[node].data);
}
if(T[node].left!=Null)q.push(T[node].left);
if(T[node].right!=Null)q.push(T[node].right);
}
} int main(){
Tree root=BuildTree(T);
ListTraverse(root); return ;
}

List Leaves 树的层序遍历的更多相关文章

  1. 剑指offer面试题23:从上到下打印二叉树(树的层序遍历)

    题目:从上往下打印出二叉树的每个节点,同一层的结点按照从左往右的顺序打印. 解题思路:二叉树的层序遍历,在打印一个节点的时候,要把他的子节点保存起来打印第一层要把第二层的节点保存起来, 打印第二层要把 ...

  2. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

    题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...

  3. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

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

  4. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  5. PTA L2-006 树的遍历-二叉树的后序遍历+中序遍历,输出层序遍历 团体程序设计天梯赛-练习集

    L2-006 树的遍历(25 分)   给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤),是二叉树中结点的 ...

  6. PTA 树的遍历(根据后序中序遍历输出层序遍历)

      给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式:输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行给出其后序遍历序列.第 ...

  7. PTA 7-10 树的遍历(二叉树基础、层序遍历、STL初体验之queue)

    7-10 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数 ...

  8. 利用层序遍历(含空节点)和中序遍历重建二叉树 python

    给定一颗二叉树的层序遍历(不含None的形式)和中序遍历序列,利用两个序列完成对二叉树的重建. 还是通过一个例子来说明整个过程,下图所示的二叉树,层序遍历结果为[a,b,c,d,e],中序遍历结果为[ ...

  9. LeetCode 637. Average of Levels in Binary Tree(层序遍历)

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

随机推荐

  1. IntelliJ IDEA SVN无法正常使用问题

    SVN checkout时候会出现错误:Cannot run program "svn" (in directory "E:\Projects"): Creat ...

  2. 404 Note Found 队-Alpha 事后诸葛亮

    目录 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队的角色,管理,合作 总结: 本小组和其他组的评分 分工和贡献分 全组讨论的照片 问题 第一组提问回答:爸爸饿了队 第二组提问回答:拖 ...

  3. 05_Dockerfile实战(上)

    在上一章我们介绍了docker镜像的概念,并且介绍了构建镜像的两种方式 使用docker commit命令提交创建的容器. 使用Dockerfile文件和docker build命令,这种更为推荐和常 ...

  4. 查询优化百万条数据量的MySQL表

    转自https://www.cnblogs.com/llzhang123/p/9239682.html 1.两种查询引擎查询速度(myIsam 引擎 ) InnoDB 中不保存表的具体行数,也就是说, ...

  5. 在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all

    在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all 前天来了个新需求, 有一个授权书的文件要点击下载, 需要在前端生成, ...

  6. MongoDB DBA 实践3-----安装mongdb4.0发生错误

    在安装mongodb时,常常会出现一些错误,导致无法安装完全,下面则是各种错误与它们的解决方法: 1.在window机安装mongodb4.0, 其中的一种:由于缺失系统补丁,而导致无法完全安装,具体 ...

  7. python教程(五)·列表与元组

    在介绍了通用的序列操作后,我们来学习序列类型中的列表和元组 列表 回顾 我们已经初步学习了列表,在深入之前,让我们简单回顾一下以往的知识. 创建列表的方法: >>> list_1 = ...

  8. Python3 图像识别(一)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.环境准备: 1.Python3.x(我是用的是Python3.6.5),这个问题不大,只要3.4以上就OK. ...

  9. 修改并编译golang源码

    最近为了做Hyperledger Fabric国密改造,涉及到了golang源码的改动.特将操作过程整理如下,以供参考: golang的源码安装其实比较简单,只需运行源码包中的脚本src/all.ba ...

  10. 【转】 GATK--原始数据预处理

    1. 对原始下机fastq文件进行过滤和比对(mapping) 对于Illumina下机数据推荐使用bwa进行mapping. Bwa比对步骤大致如下: (1)对参考基因组构建索引: 例子:bwa i ...