03-树2 List Leaves(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 NN (\le 1010) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1N1. Then NN 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
题目很简单,就是建树,并按层序层序遍历的顺序输出叶子节点。

建立一个队列,先入队根节点,然后出队,入根节点的子节点,接下来每次出队都将它的子节点入队。只要在出队的时候判断是否为叶子节点并输出即可

package week32;
import java.util.*;

class TreeNode{
	public int data;
	public int left;
	public int right;
	public int check=0;
}
public class Main {
	private static Scanner in;
	//寻找根节点
	static int findRoot(TreeNode[] t){
		int root=0;
		for(int i=0;i<t.length;i++){
			if(t[i].check==0)
				root=i;
		}
		return root;
	}
	//判断是否叶子节点
	static boolean leaves(TreeNode node){
		if(node.left==-1&&node.right==-1){
			return true;
		}
		else{
			return false;
		}	
	}
	//建树
	public static TreeNode[] buildTree(){
		int num=Integer.parseInt(in.nextLine());//注意不能用nextint
		TreeNode[] tree=new TreeNode[num];
		for(int i=0;i<num;i++){
			TreeNode a = new TreeNode();
			String s=in.nextLine();
			if(s.substring(0, 1).equals("-")){
				a.left=-1;
			}else{
				a.left=Integer.parseInt((s.substring(0, 1)));
			}
			if(s.substring(2,3).equals("-")){
				a.right=-1;
			}else{
				a.right=Integer.parseInt((s.substring(2, 3)));
			}			//输入为-则设为-1.否则输入数值
			a.data=i;
			tree[i]=a;
		}
		for(int i=0;i<num;i++){
			if(tree[i].left!=-1){
				tree[(tree[i].left)].check=-1;	
			}
			if(tree[i].right!=-1){
				tree[(tree[i].right)].check=-1;
			}
		}
		return tree;
	}
	层序遍历入队,输出时判断即可
	public static void cengXu(TreeNode[] tree){
		Queue<TreeNode> queue=new LinkedList<TreeNode>();
		int root=findRoot(tree);
		queue.offer(tree[root]);
		int l=tree.length;
		boolean first=true;
		while(l!=0){
			TreeNode t=queue.poll();
			if(leaves(t)){
				if(first){
					first=false;
					System.out.print(t.data);
				}
				else
					System.out.print(" "+t.data);
			}
			else{
				if(t.left!=-1)
					queue.offer(tree[t.left]);
				if(t.right!=-1)
					queue.offer(tree[t.right]);
			}
			l--;
		}
	
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		in=new Scanner(System.in);
		TreeNode[] tree=buildTree();
		cengXu(tree);
		
	}

}
写的有些啰嗦,如果有人看了能请改进意见,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值