ListLeaves问题 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−1. 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

解决思路:
1.用静态链表来存储节点,若孩子为“-”,则标记为-1.返回root在数组中的位置。
2.因为题目要求从上到下,从左到又,这需要按层遍历的方法,因此用数组来创建一个队列。
3.先在队列中输入根节点,开启循环。输出根节点,判断节点左右孩子是否为-1,若真,打印根节点,若不是,判断左孩子是否为-1,若不是,左孩子入队,同样的操作右孩子。依次循环
import java.util.Scanner;
class Node //创建节点类型
{
    int key;
    int left;
    int right;
    
}
class queue //创建队列类型
{
    int front;
    int rear;
    int num;
    int maxsize;
    int[] arrQ;

    public queue(int N) //初始化队列
    {
        maxsize=N;
        front=0;
        rear=-1;
        num=0;
        arrQ=new int[maxsize];
    }
    public void insert(int value)
    {
        if (rear==maxsize-1)
        {
            throw new RuntimeException("full");
        }
        else
        {
            rear=rear+1;
            arrQ[rear]=value;
            num++;
        }

    }
    public int remove()
    {
        int temp=arrQ[front];
        arrQ[front]=0;
        front++;
        if (front==maxsize)
        {
            front=0;
        }
        num--;
        return temp;
    }
    public boolean isEmpty()
    {
        return num==0;
    }



}
class test //主类型
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        Node[] arr=new Node[8];
        int head=biuldTree(arr,sc);
        ListLeaves(arr,head);

        
        
    }
    
    
    public static int biuldTree(Node[] arr,Scanner sc)
    {
        int N=sc.nextInt();
        String templeft;
        String tempright;
        int[] flag=new int[N];
        int root=0;
        
        for (int i=0;i<N ;i++ )
        {
            Node node=new Node();
            templeft=sc.next();
            tempright=sc.next();
            arr[i]=node;
            if (templeft.equals("-"))
            {
                node.left=-1;
            }
            else
            {
                node.left=Integer.parseInt(templeft);
                flag[node.left]=1;
            }
            if (tempright.equals("-"))
            {
                node.right=-1;
            }
            else
            {
                node.right=Integer.parseInt(tempright);
                flag[node.right]=1;
            }
        }
        for (int i=0;i<N ;i++ )
        {
            if (flag[i]==0)
            {
                root=i;
            }
        }
        return root;
    }
    public static void ListLeaves(Node[] arr,int head)
    {

        queue arrQ=new queue(8);
        arrQ.insert(head);
        int a=0;
        while (!arrQ.isEmpty())
        {
            head=arrQ.remove();
            if (arr[head].left==-1&&arr[head].right==-1)
            {
                if (a==0)
                {
                    System.out.print(head);
                }
                else
                    System.out.print(" "+head);
                a++;
            }
            if (arr[head].left!=-1)
            {
                arrQ.insert(arr[head].left);
            }
            if (arr[head].right!=-1)
            {
                arrQ.insert(arr[head].right);
            }
        }
        

    }
}

打印结果

 

posted on 2018-04-25 17:23  yfyfyf947  阅读(117)  评论(0编辑  收藏  举报