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 N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N 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

#include<iostream>
#include<queue>
using namespace std;
#define N 10
#define Null -1
struct node{
	int left;
	int right;
}T[N];
int check[N];
int buildtree(struct node T[]){  //建立二叉树
	int n,i;
	int root = Null;
	cin>>n;
	for(i = 0; i < n; i++) check[i]=0;
	for(i = 0; i < n; i++){
		char left,right;
		cin>>left>>right;
		if(left != '-'){
			T[i].left = left - '0';
			check[T[i].left] = 1;
		}else{
			T[i].left = Null;
		}
		if(right != '-'){
			T[i].right = right - '0';
			check[T[i].right] = 1;
		}else{
			T[i].right = Null;
		}
	}
	for(i = 0; i < n; i++){
		if(!check[i]) break;
	} 
	root = i;
	
	return root;
}
//层序遍历,打印叶结点 
void LevelOrder(int r)
{
	if(r==Null) return;
	int cnt = 0;
	queue<int>q;
	q.push(r);
	while(!q.empty()){
		int temp = q.front();
		q.pop();
		if(T[temp].left == Null && T[temp].right == Null) //如果是叶子节点就输出
		{
			if(cnt) cout<<" ";
			cout<<temp;
			cnt++;
		}
		
		if(T[temp].left!=Null) q.push(T[temp].left);
		if(T[temp].right!=Null) q.push(T[temp].right);
	}
}


int main()
{
	int R = buildtree(T);
	LevelOrder(R);
} 

java代码


import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

class node{
	int left;
	int right;
}

public class Main {
		static final int N = 10;
		static final int Null = -1;
		static Scanner sc = new Scanner(System.in);
		static node p[] = new node[N];
		static int check[] = new int[N];
		static int creat() {
			int root = Null;
			int n = sc.nextInt();
			int i ;
			if(n != 0) {
				for(i = 0; i < n; i++) check[i] = 0;
				for(i = 0; i < n; i++) {
					p[i] = new node();
					char left = sc.next().charAt(0);
					char right = sc.next().charAt(0);
					if(left != '-') {
						p[i].left = left - '0';
						check[p[i].left] = 1;
					}else {
						p[i].left = Null;
					}
					if(right != '-') {
						p[i].right = right - '0';
						check[p[i].right] = 1;
					}else {
						p[i].right = Null;
					}
				}
				for(i = 0; i < n; i++) {
					if(check[i] == 0) break;
				}
				root = i;
			}
			return root;
		}
		static void LevelOrder(int r) {
			int cnt = 0;
			if(r == Null) return ;
			LinkedList<Integer> q = new LinkedList<Integer>();
			q.add(r);
			while(!q.isEmpty()) {
				int temp = q.removeFirst();
				if(p[temp].left == Null && p[temp].right == Null) {
					if(cnt != 0) System.out.print(" ");
					System.out.print(temp);
					cnt++;
				}
				
				if(p[temp].left != Null) q.add(p[temp].left);
				if(p[temp].right != Null) q.add(p[temp].right);
			}
		}
		
		public static void main(String[] args) throws IOException {
			int r = Null;
			r = creat();
			LevelOrder(r);
		}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值