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. Loadrunner之HTTP接口测试

    Loadrunner之HTTP接口测试 接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过程. ...

  2. JNI由浅入深_6_简单对象的应用

    1.声明native方法 public class ComplexObject { /** * 返回一个对象数组 * @param val * @return */ public native Per ...

  3. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  4. HDU 1213(裸并查集)(无变形)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/ ...

  5. 【Step By Step】将Dotnet Core部署到Docker上

    本教程的前提是,你已经在Linux服务器上已经成功的安装了Docker,我会大概介绍在此过程中用到的Docker命令,并不会介绍所有的Docker命令(因为我也不会). 一.在Docker中运行Dot ...

  6. oracle自动冷备份脚本

    根据自己网上的资料和自己的需求,写的oracle冷备份脚本. 整体思路: 1.停止服务 2.文件拷贝 3.启动服务 保存以为文件为BAT格式,点击可以用下. rem ----------------- ...

  7. javascript设计模式系列二-封装

    JavaScript封装: var Book = function (id, name, price) { this.id = id, this.name = name, this.price = p ...

  8. MySQL数据查询(重点)

    1.查询所有列   * 为所有列 select * from table_name; 2.查询指定列 select id,age from table_name; 3.查询时添加常量列-------本 ...

  9. vue-scroller使用

    <template> <div class="page page-scroller"> <scroller class="scroller& ...

  10. hive在客户机启动时出现的问题

    运行环境:一拖一分布式集群+客户端 mysql和hive安装在客户机上 问题:在客户机终端启动hive时出现如下问题: 目前还没有找出是什么原因! 解决办法: 参照http://dblab.xmu.e ...