7-4 List Leaves (25分) JAVA
Posted Ethan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章