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. HDU 1272小希的迷宫(裸并查集,要判断是否构成环,是否是连通图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    ...

  2. 记如何用树莓派3开一个无线AP

    开热点 忘掉自己手动配置吧 create_ap git clone git@github.com:oblique/create_ap.git cd create_ap sudo make instal ...

  3. 如何安装使用MinDoc搭建个人在线wiki文档

    MinDoc是什么? MinDoc是一个在线的文档管理系统,该系统适用于团队.个人等使用.开发者最初的目的是为了便于公司内部使用,仿照看云开发.有laravel版本以及golang版本.不过larav ...

  4. php图片上传存储源码,可实现预览

    <?php header("content-Type: text/html; charset=gb2312"); $uptypes=array('image/jpg', // ...

  5. Business Unit Helper

    using System; using System.Linq; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Sy ...

  6. oracle相关操作,存储、临时表空间、用户操作、启动过程

    表空间:此空间是用来进行数据存储的(表.function.存储过程等),所以是实际物理存储区域.临时表空间:主要用途是在数据库进行排序运算[如创建索引.order by及group by.distin ...

  7. c#开发微信公众号——关于c#对象与xml的转换

    在成为微信公众号开发者以后,整个交互流程:用户->微信服务器->自己的服务器->返回微信服务器->用户: 举个例子:用户在微信公众号里面发了个“您好!”,微信服务器会以特定的x ...

  8. hadoop 提交程序并监控运行

    程序编写及打包 使用maven导入第三方jar pom.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  9. 【原创】frozenset集合函数入门及实例

    函数作用 frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素.与之对应的是set函数,set无序排序且不重复,是可变的,有add(),remove()等方法. 函数原型 f ...

  10. Windows下安装配置MinGW GCC调试环境

    下载安装文件:Sourceforge  64位系统安装选项记得选x86_64.安装过程中连不上服务器的话也可以选择下载压缩包. 配置环境变量,假设mingw安装目录为C:\mingw-w64\ming ...