7-4 List Leaves (25分) JAVA
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的更多相关文章
- 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 ...
- 7-4 是否同一棵二叉搜索树 (25分) JAVA
给定一个插入序列就可以唯一确定一棵二叉搜索树.然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到. 例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结 ...
- 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 ...
- 浙大数据结构课后习题 练习三 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 ...
- PTA - - 06-图1 列出连通集 (25分)
06-图1 列出连通集 (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- 25个Java机器学习工具和库
本列表总结了25个Java机器学习工具&库: 1. Weka集成了数据挖掘工作的机器学习算法.这些算法可以直接应用于一个数据集上或者你可以自己编写代码来调用.Weka包括一系列的工具,如数据预 ...
- PTA 字符串关键字的散列映射(25 分)
7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...
- PTA 旅游规划(25 分)
7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...
随机推荐
- 50个SQL语句(MySQL版) 问题六
--------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...
- vnc远程工具的使用,Windows系统下VNC远程工具的使用教程
服务器管理工具可以作为VNC的客户端进行VNC的相关操作,是一款功能强大的VNC客户端软件!同时,它也可以作为FTP的客户端,来进行FTP的相关操作!它能够连接Windows和Linux系统下的服务器 ...
- Rocket - debug - SBA
https://mp.weixin.qq.com/s/eFOHrEhvq2PlEJ14j2vlhg 简单介绍SBA的实现. 1. SystemBusAccessState 系统总线访问状态: 分别是: ...
- Rocket - util - MultiWidthFifo
https://mp.weixin.qq.com/s/CUnrpyQN5LRBR5bxC5u86A 简单介绍MultiWidthFifo的实现. 1. 基本介绍 实现一个输入宽度 ...
- 【Storm】核心组件nimbus、supervisor、worker、executor、task
nimbus 是整个集群的控管核心,负责topology的提交.运行状态监控.任务重新分配等工作. zk就是一个管理者,监控者. 总体描述:nimbus下命令(分配任务),zk监督执行(心跳监控wor ...
- (Java实现) 洛谷 P1106 删数问题
题目描述 键盘输入一个高精度的正整数NN(不超过250250位) ,去掉其中任意kk个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的NN和kk,寻找一种方案使得剩下的数字组成的新数最小 ...
- Java实现 LeetCode 738 单调递增的数字(暴力)
738. 单调递增的数字 给定一个非负整数 N,找出小于或等于 N 的最大的整数,同时这个整数需要满足其各个位数上的数字是单调递增. (当且仅当每个相邻位数上的数字 x 和 y 满足 x <= ...
- SOA架构和微服务架构的区别与特点
1.SOA架构和微服务架构的区别 首先SOA和微服务架构一个层面的东西,而对于ESB和微服务网关是一个层面的东西,一个谈到是架构风格和方法,一个谈的是实现工具或组件. 1.SOA(Service Or ...
- 基本的bash shell 命令
1.遍历目录:cd 2.显示目录列表:ls 3.创建文件:touch 4.复制文件:cp 5.链接文件:ln 6.重命名文件:mv 7.删除文件:rm 8.创建目录:mkdir 9.删除目录:rmdi ...
- (一)c++之细解 const 与 static
const成员变量与const成员函数与const对象 static成员变量与static成员函数与static全局变量 const成员变量 1. const用于类中成员变量时,将类成员变为只读属性( ...