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);
}
}
本文介绍了一种算法,用于按从上到下、从左到右的顺序列出二叉树的所有叶子节点。通过输入节点数量和各节点的左右子节点信息,构建二叉树并使用层序遍历来找出并打印所有叶子节点的索引。
231

被折叠的 条评论
为什么被折叠?



