7-4 List Leaves (25分) JAVA
2021-05-12 14:27
标签:ase port out list() lines edr code first next Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. 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. 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. 题意: 给定一个树,从上到下,从左往右遍历出该树的叶子节点。 由此可知,我们需要对树进行层序遍历。 思路: 1.建树,通过数组存储树的每个节点。数组的元素为 Node类,该类包含节点值element,左孩子编号left,右孩子编号right, 2.对二叉树进行层序遍历,在遍历的过程中记录叶节点(左右孩子编号均为 - 的节点为叶节点),最后打印叶节点。 7-4 List Leaves (25分) JAVA 标签:ase port out list() lines edr code first next 原文地址:https://www.cnblogs.com/ethan-37/p/13140268.htmlInput Specification:
Output Specification:
Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
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
ArrayList
for (int i = 0; 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 ) {
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 ) {
//设置标记,先将每个节点标记为 -1
//当节点编号出现在孩子节点编号时,将标记设为1
check[i]=-1;
}
for (int i = 0; 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 ) {
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
文章链接:http://soscw.com/index.php/essay/84717.html