PAT 03-树2 List Leaves(Java实现)

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.

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


思路分析:建立一棵树,每行代表一个结点,序号从0~N(N < 9 ),‘ - ’ 代表没有孩子。首先找到树的根,然后依次从根开始查找当前结点的左孩子和右孩子,并加入到队列中,每次循环队列出队一个结点,如果该结点都没有左右孩子,则输出。(BFS,即层序遍历的思想)。可以不用建立一棵树,直接用数组模拟,这样更方便一些。





package Test1216;
import java.util.Scanner;

class TreeNode{
	int key;
	int left;
	int right;
}

class Queue {
	int[] arr;
	int front = 0;
	int rear = -1;
	int size;
	int num = 0;
	int tmp;

	Queue(int size) {
		this.size = size;
		arr = new int[size];
	}

	public void add(int value) {
		if (!isFull()) {
			if (rear == size - 1)
				rear = -1;
			arr[++rear] = value;
			num++;
		} else
			System.out.println("队列满,不能添加元素");
	}

	public int remove() {
		if (!isEmpty()) {
			num--;
			if (front != size - 1) {
				return arr[front++];
			} else {
				tmp = arr[front];
				front = 0;
				return tmp;
			}
		} else {
			System.out.println("队列空,不能删除元素");
			return -1;
		}

	}

	public boolean isEmpty() {
		if (num == 0)
			return true;
		return false;
	}

	public boolean isFull() {
		if (num == size)
			return true;
		return false;
	}

}

public class ListLeaves {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int tmp;
		TreeNode[] node = new TreeNode[N];
		int head = read(node, sc, N);
		
		 Queue queue=new Queue(N);
		 queue.add(head);
		 while(!queue.isEmpty()){
		 tmp=queue.remove();
		 if(node[tmp].left==-1 && node[tmp].right==-1)
		 System.out.print(tmp+" ");
		 if(node[tmp].left!=-1){
		 queue.add(node[tmp].left);
		 }
		 if(node[tmp].right!=-1){
		 queue.add(node[tmp].right);
		 }
		 }

	}

	public static int read(TreeNode[] node, Scanner sc, int N) {
		String tmpLeft;
		String tmpRight;
		int root = -1;
		int[] flag = new int[N];
		for (int i = 0; i < N; i++) {
			TreeNode tmpNode=new TreeNode();
			tmpNode.key = i;
			tmpLeft = sc.next();
			tmpRight = sc.next();
			node[i]=tmpNode;
			if (tmpLeft.equals("-")) {
				node[i].left = -1;
			} else {
				node[i].left = Integer.parseInt(tmpLeft);
				flag[node[i].left] = 1;
			}
			if (tmpRight.equals("-")) {
				node[i].right = -1;
			} else {
				node[i].right = Integer.parseInt(tmpRight);
				flag[node[i].right] = 1;
			}
		}
		for (int i = 0; i < N; i++) {
			if (flag[i] == 0)
				root = i;
		}
		return root;

	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值