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. MyBatis保存完整日期的解决方法

    在用mybatis时,对mysql数据库是datatime字段添加值是,发现添加成功后查看数据库字段值是,只有年月日有值,时分秒则为0来表示的,更改为java.sql.date,time等也不行,如果 ...

  2. IIS - 虚拟目录与应用程序的异同

    在Windows 7 IIS7中,对服务器建立站点后,有二种添加子站点的方式 A. 虚拟目录 B. 应用程序   简单总结下二者之间的异同 A.虚拟目录     虚拟目录是指在站点下建立一个虚拟子目录 ...

  3. C#利用反射动态创建对象 带参数的构造函数和String类型 (转载)

    最近笔者有一个想法需要利用反射动态创建对象(如string,int,float,bool,以及自定义类等)来实现,一直感觉反射用不好,特别是当构造函数带参数的时候.MSDN上给出的例子十分复杂,网上的 ...

  4. 硬盘分区表知识——详解硬盘MBR

    这片文章说得很详细,原文:http://hi.baidu.com/waybq/blog/item/3b8db64bef3dc7f583025c66.html --------------------- ...

  5. python2.7下使用logging模块记录日志到终端显示乱码问题解决

    刚才翻了翻2年以前用python2.7写的一个爬虫程序,主要功能就是把各地市知识产权局/专利局网站的专利相关项目.课题通知,定期爬取和分析,辅助企业进行项目申请. 这里要谈的不是爬虫功能的实现,而是今 ...

  6. Unity各平台内置宏定义

    属性 方法 UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code. UNITY_EDI ...

  7. JQuery模拟网页中自定义鼠标右键菜单

    题外话.......最近在开发一个网站项目的时候,需要用到网页自定义右键菜单,在网上看了各路前辈大神的操作,头晕目眩,为了达到目的,突然灵机一动,于是便有了这篇文章. 先放个效果图(沾沾自喜,大神勿喷 ...

  8. jquery购物车添加功能

    <html> <head> <meta charset="UTF-8"> <title></title> <scr ...

  9. mysql 基本的操作数据库命令

    注意:命令操作都是分号结尾 1 .连接mysql: mysql -u  用户名   -p 密码 2.展示所有数据库: show  databases; 3.进入数据库: use   数据库名字; 4. ...

  10. nodejs知识点

    rss(resident set size):所有内存占用,包括指令区和堆栈. heapTotal:”堆”占用的内存,包括用到的和没用到的. heapUsed:用到的堆的部分. external: V ...