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. 分布式 ID 的 9 种生成方式

    为什么要用分布式ID? 在说分布式ID的具体实现之前,我们来简单分析一下为什么用分布式ID?分布式ID应该满足哪些特征? 什么是分布式ID? 拿MySQL数据库举个栗子: 在我们业务数据量不大的时候, ...

  2. 【朝夕技术专刊】RabbitMQ路由解析(上篇)

    欢迎大家阅读<朝夕Net社区技术专刊> 我们致力于.NetCore的推广和落地,为更好的帮助大家学习,方便分享干货,特创此刊!很高兴你能成为忠实读者,文末福利不要错过哦! 上篇文章介绍了如 ...

  3. ES6-常用四种数组

    1.map 1.1 个人理解 映射 一个对一个 例如:[45,57,138]与[{name:'blue',level:0},{name:'zhangsan',level:99},{name:'lisi ...

  4. Redis 的原理与应用场景及数据库关系

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一. Redis 是什么? Redis是一个开源的使用ANSIC语言编写.支持网络.单进程单线程.可基于 ...

  5. ASP.NET生成验证码

    首先,添加一个一般处理程序 注释很详细了,有不懂的欢迎评论 using System; using System.Collections.Generic; using System.Drawing; ...

  6. Java实现 LeetCode 407 接雨水 II(二)

    407. 接雨水 II 给定一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水. 说明: m 和 n 都是小于110的整数.每一个单位的高 ...

  7. Java实现 LeetCode 399 除法求值

    399. 除法求值 给出方程式 A / B = k, 其中 A 和 B 均为代表字符串的变量, k 是一个浮点型数字.根据已知方程式求解问题,并返回计算结果.如果结果不存在,则返回 -1.0. 示例 ...

  8. Java实现 蓝桥杯VIP 算法提高 解二元一次方程组

    算法提高 解二元一次方程组 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个二元一次方程组,形如: a * x + b * y = c; d * x + e * y = f; x,y代 ...

  9. Java实现 LeetCode 29 两数相除

    29. 两数相除 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商 ...

  10. postman接口超时设置,用于debug等断点调试

    Settings->General->Request Timeout in ms(0 for infinity):设置请求超时的时间,默认为0