PTA数据结构-03-树2 List Leaves

一、题目

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

 

二、解答

import java.util.*;
//采用队列结构,就可以实现按层遍历
public class Main {
    static ArrayList<Integer> queue = new ArrayList<>();
    static int front = -1;
    static int rear = -1;
    static String res = "";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Node[] tree = createTree(sc);
        findLeaves(findRoot(tree), tree);
        System.out.println(res.trim());
    }
    public static void findLeaves(int index, Node[] tree){
        //如果不存在左右儿子,则判为叶子
        if (tree[index].left + tree[index].right == -2) res += index + " ";
        else {
            if (tree[index].left != -1) inQue(tree[index].left);
            if (tree[index].right != -1) inQue(tree[index].right);
        }
        int next = outQue();
        if (next != -1) findLeaves(next, tree);
    }
    public static int findRoot(Node[] tree){
        //考虑空树
        if (tree == null) return -1;
        //java数组初始化默认为0
        int[] vistited = new int[tree.length];
        for (Node node:tree) {
            if (node.left != -1) vistited[node.left] = 1;
            if (node.right != -1) vistited[node.right] = 1;
        }
        int root = 0;
        for (int i = 0; i < tree.length; i++) {
            if (vistited[i] == 0){ root=i; break;}
        }
        return root;
    }
    public static void inQue(int num){
        queue.add(num);
        front++;
    }
    public static int outQue(){
        if (rear >= front) return -1;
        return queue.get(++rear);
    }
    public static Node[] createTree(Scanner sc){
        //注意不要混用sc.nextLine和sc.nextInt,此时如果使用了sc.nextInt,则下一句的sc.nextLine将会扫上来一个回车
        int size = Integer.parseInt(sc.nextLine());
        //考虑空树
        if (size == 0) return null;
        Node[] arr = new Node[size];
        for (int i = 0; i < size; i++) {
            String[] strs = sc.nextLine().split(" ");
            if (strs[0].equals("-")) strs[0] = "-1";
            if (strs[1].equals("-")) strs[1] = "-1";
            //Node[]在创建的时候里面全为空,必须要new新的Node存放到Node[]中
            arr[i] = new Node();
            arr[i].left = Integer.parseInt(strs[0]);
            arr[i].right = Integer.parseInt(strs[1]);
        }
        return arr;
    }
}
class Node{
    int left = -1;
    int right = -1;
}

思路:

  1. 关键点:按层遍历树该如何实现?采用队列结构
  2. 解题:如何构建树,如何找到树根,如何遍历树
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值