Code

래벨을 기준으로 탐색하는 코드.

각 레벨이 루트노드에서 다른 노드로 도달하는 거리이다.

public class Main {
    Node root;
    SeqLockPrefixTree.Node test;

    public void BFS(Node root) {
        Queue<Node> q = new LinkedList<>();

        q.offer(root);
        int level = 0; // 레벨이 곧 루트노드에서 다른 노드로 도달할 수 있는 간선 수 == 거리
        while (!q.isEmpty()) {
            int length = q.size();
            System.out.println(level + " : ");
            for (int i = 0; i < length; i++) {
                Node current = q.poll();
                System.out.println(current.data + " ");
                if (current.lt != null) q.offer(current.lt);
                if (current.rt != null) q.offer(current.rt);
            }

            level++;
            System.out.println();
        }
    }
}

Introduction to Java Algorithm Problem Solving: Coding Test Preparation| codingcamp - Course on Inflearn