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 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

题意:

给定一个树,从上到下,从左往右遍历出该树的叶子节点。

由此可知,我们需要对树进行层序遍历。

思路:

1.建树,通过数组存储树的每个节点。数组的元素为 Node类,该类包含节点值element,左孩子编号left,右孩子编号right,

2.对二叉树进行层序遍历,在遍历的过程中记录叶节点(左右孩子编号均为 - 的节点为叶节点),最后打印叶节点。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; /**
* @author Ethan
* @date 2020/6/16
* List Leaves
* 思路:
* 1.对树进行层序遍历,在遍历的过程中记录叶节点。
* 2.打印记录的叶节点
*/
public class Main1 {
static int root = -1;
public static void main(String[] args)throws Exception {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
Node[] nodes = buildTree(in);
//层序遍历通过队列实现,LinkedList类具有队列的特性
LinkedList<Node> queue = new LinkedList();
if (root!=-1){
//将根节点加入队列
queue.add(nodes[root]);
}
ArrayList<Integer> leaves = new ArrayList();
for (int i = 0; i < nodes.length; i++) {
int n =0;
//遍历出每个节点
Node node = queue.poll();
//如果节点的左右编号相同,说明都是-1,则为叶节点
if (node.left== node.right){
leaves.add(node.element);
}
//如果左孩子节点不为空,则将左孩子节点加入队列
if (node.left!=-1){
queue.add(nodes[node.left]);
}
//如果右孩子节点不为空,则将右孩子节点加入队列
if (node.right!=-1){
queue.add(nodes[node.right]);
}
} for (int i = 0; i < leaves.size(); i++) {
System.out.print(leaves.get(i));
if (i != leaves.size() - 1) {
System.out.print(" ");
}
}
} static Node[] buildTree(StreamTokenizer in) throws Exception { in.nextToken();
//第一行为树的节点个数
int n = (int) in.nval;
Node[] tree = new Node[n]; int[] check = new int[n];
for (int i = 0; i < n; i++) {
//设置标记,先将每个节点标记为 -1
//当节点编号出现在孩子节点编号时,将标记设为1
check[i]=-1;
}
for (int i = 0; i < n; i++) {
tree[i] = new Node();
//节点的输入顺序,即为节点值
tree[i].element = i; in.nextToken();
//读取左孩子编号
//ttype = -2时,说明为数字
if (in.ttype == -2) {
tree[i].left = (int) in.nval;
//当节点编号出现在孩子节点编号时,将标记设为1
check[tree[i].left] = 1;
} else {
//当孩子节点编号为 - 时,孩子编号记为 -1
tree[i].left = -1;
} in.nextToken();
//读取右孩子编号
if (in.ttype == -2) {
tree[i].right = (int) in.nval;
check[tree[i].right] = tree[i].right;
} else {
tree[i].right = -1;
}
} //找出根节点:未在孩子编号中出现的节点编号为根节点
for (int i = 0; i < n; i++) {
if (check[i] == -1) {
root = i;
break;
}
} return tree;
}
} class Node {
public int element;
public int left;
public int right; }

7-4 List Leaves (25分) JAVA的更多相关文章

  1. PTA 03-树2 List Leaves (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves   (25分) Given a tree, you ...

  2. 7-4 是否同一棵二叉搜索树 (25分) JAVA

    给定一个插入序列就可以唯一确定一棵二叉搜索树.然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到. 例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结 ...

  3. 03-树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. I ...

  4. 浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  5. PTA - - 06-图1 列出连通集 (25分)

    06-图1 列出连通集   (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...

  6. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  7. 25个Java机器学习工具和库

    本列表总结了25个Java机器学习工具&库: 1. Weka集成了数据挖掘工作的机器学习算法.这些算法可以直接应用于一个数据集上或者你可以自己编写代码来调用.Weka包括一系列的工具,如数据预 ...

  8. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  9. PTA 旅游规划(25 分)

    7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...

随机推荐

  1. STM32读取匿名光流数据——与Guidance的光流和超声波做对比测试

    使用两个串口同时读取匿名光流和Guidance数据:用以比较两个光流的效果 Github链接:https://github.com/W-yt/YuTian_Pro/tree/master/Guidan ...

  2. 【Linux】文件权限,ssh免密登录

    1.文件/文件夹权限 例子: -rw-r--r--. 1 root root 12288 Aug 21 09:50 aliases.db drwxr-xr-x. 2 root root 4096 Au ...

  3. Blender如何设置中文界面

    废话不多说,上图 bingo!!

  4. Linux(十) —— 使用 rz 和 sz 命令上传与下载

    以CentOS 7 系统为例,一般上传下载都是使用的第三方工具,但是在操作上并不方便,每次都要找到对应的目录才可以执行上传.下载操作,比较麻烦. 而CentOS为例的 Linux 系统可以通过安装 插 ...

  5. Java实现 蓝桥杯 算法提高 概率计算

    算法提高 概率计算 时间限制:1.0s 内存限制:256.0MB 问题描述 生成n个∈[a,b]的随机整数,输出它们的和为x的概率. 输入格式 一行输入四个整数依次为n,a,b,x,用空格分隔. 输出 ...

  6. Java实现k个数乘(cheng)(自然数的k乘积问题)

    k个数乘(cheng) 题目描述 桐桐想把一个自然数N分解成K个大于l的自然数相乘的形式,要求这K个数按从小到大排列,而且除了第K个数之外,前面(K-l)个数是N分解出来的最小自然数.例如:N=24, ...

  7. Java实现 LeetCode 216. 组合总和 III(三)

    216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...

  8. Java实现 蓝桥杯VIP 算法提高 贪吃的大嘴

    算法提高 贪吃的大嘴 时间限制:1.0s 内存限制:256.0MB 问题描述 有一只特别贪吃的大嘴,她很喜欢吃一种小蛋糕,而每一个小蛋糕有一个美味度,而大嘴是很傲娇的,一定要吃美味度和刚好为m的小蛋糕 ...

  9. Java实现 LeetCode 96 不同的二叉搜索树

    96. 不同的二叉搜索树 给定一个整数 n,求以 1 - n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 1 3 ...

  10. Python API自动化测试实操

    废话不多说,直接上代码截图: 我们首先来看看整个工程的目录结构,这样以便于了解项目的调用关系:config   #这里是配置包 -- base_url.py 具体配置了被测系统的url and pat ...