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. ASP.NET如何批量保存动态生成的文本框?

    对于OA系统,表单签核功能必不可少.而根据公司的情况,表单自然又五花八门,所以就要求能够让用户自己建立表单并设定表单的流程.填写内容等等.我之前写过一篇文章[地址:pivot的用法(SQL SERVE ...

  2. linux 常用进程使用命令

    查看进程占用pid ps aux | grep program_filter_word 杀死pid kill -

  3. 解决Js跨域访问的问题

    1,最近有个需求,用Js获取Html标签<input type="file"/>的路径!遇到代码拒绝访问,提示安全验证,不允许跨域访问,简单的设置一下浏览器即可,不过对 ...

  4. kvo本质探寻

    一.概述 1.本文章内容,须参照本人的另一篇博客文章“class和object_getClass方法区别”加以理解: 2.基本使用: //给实例对象instance添加观察者,监听该实例对象的某个属性 ...

  5. 数字转汉字|语言代码|NSNumberFormatter

    iOS之阿拉伯数字转中文数字 - 简书 iOS中金额数字的格式化 NSNumberFormatter - 简书 ISO语言代码(ISO-639)与国家代码(ISO-3166) - CSDN博客 语种名 ...

  6. [NOI2015]软件包管理器(树链剖分,线段树)

    题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个 ...

  7. MySQL——安装

    1. 下载源: http://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/mysql80-community-release-el7-2.no ...

  8. 接口测试jemeter使用

    使用jemeter5时要先添加环境变量,需要有JDK1.8及以上版本支持.这里主要对接口测试做一些说明. 以上就是常见的设置问题.在window上我们通常是不需要改动配置文件的,如果要在生产上执行测试 ...

  9. 第三章:文件I/O

    本章开始讨论UNIX系统的文件I/O函数,包括打开文件.读文件.写文件等. UNIX系统中的大多数文件I/O只需要用到5个函数:open.read.write.lseek和close.它们每执行一次都 ...

  10. pgsql 变量赋值方法

    1.网上一般说的方法如下: :=,赋值,比如user_id := 20; select into 赋值,比如 SELECT INTO myrec * FROM emp WHERE empname = ...