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 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...
随机推荐
- SpringAOP使用及源码分析(SpringBoot下)
一.SpringAOP应用 先搭建一个SpringBoot项目 <?xml version="1.0" encoding="UTF-8"?> < ...
- DataGuard VS Beedup & GoldenGate灾备方案参数对比
世上本无完美产品,只有合适的才是最好的! 用户重视灾备数据站点的建设,毋庸置疑必备品.如果考虑带宽及事务完整性保证,存储灾备和操作系统级灾备局限性显而易见. 商用价值一般用于解决数据库自带辅助功能的短 ...
- Java实现 LeetCode 808 分汤 (暴力模拟)
808. 分汤 有 A 和 B 两种类型的汤.一开始每种类型的汤有 N 毫升.有四种分配操作: 提供 100ml 的汤A 和 0ml 的汤B. 提供 75ml 的汤A 和 25ml 的汤B. 提供 5 ...
- Java 第十一届 蓝桥杯 省模拟赛 最大的元素距离
在数列 a_1, a_2, -, a_n中,定义两个元素 a_i 和 a_j 的距离为 |i-j|+|a_i-a_j|,即元素下标的距离加上元素值的差的绝对值,其中 |x| 表示 x 的绝对值. 给定 ...
- Java实现 蓝桥杯 猜算式
猜算式 看下面的算式: □□ x □□ = □□ x □□□ 它表示:两个两位数相乘等于一个两位数乘以一个三位数. 如果没有限定条件,这样的例子很多. 但目前的限定是:这9个方块,表示1~9的9个数字 ...
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
- (七)DVWA之SQL Injection--SQLMap测试(Low)
目录结构 一.测试需求分析 二.SQLMap利用SQL注入漏洞,获取数据库信息 1.判断是否存在注入点 2.获取DBMS中所有的数据库名称 3.获取Web应用当前连接的数据库 4.列出数据库中的所有用 ...
- Centos7 安装 redis6 的部分问题总结
首先把redis.tar.gz 解压到你想要的路径 检查一下安装环境: yum -y install gcc yum -y install epel-release 执行 make 和 make in ...
- activeMQ从入门到简单集群指南
1.什么是amq MQ是消息中间件,基于JAVA的JMS消息服务机制来传递信息. 2.mq的作用 MQ给程序之间提供了一个缓冲,避免了在程序交互频繁的情况下,提高程序性能瓶颈和数据的可靠性 3.mq怎 ...
- 关于wifi营销的看过来
亲测可用.对于一个开发者来说,终于如获至宝.详情联系qq2455994690.源码可二开.包括微信一键关注上网,手机验证码上网.