0

Queue queue = new LinkedList<>();

Full method in which i am creating queue object.

public ArrayList<ArrayList> levelOrder(Node root) { ArrayList<ArrayList> list = new ArrayList<>();

    if (root == null) {
        return list;
    }

    Queue<Node> queue = new LinkedList<>();
    queue.add(root);

    while (!queue.isEmpty()) {
        int size = queue.size();
        ArrayList<Integer> level = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            Node node = queue.poll();
            level.add(node.value);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
        list.add(level);
    }
    return list;
}

when ever i try open queue methods like queue.add() it opens Queue interface not LinkedList Class in intellij IDEA.

I am trying to understand why are we using Linked List while creating queue object.

vampcarti
  • 1
  • 2
  • `Queue` is an interface (meaning `new Queue<>();` doesn't work) and `LinkedList` is the actual implementation of the interface. – OH GOD SPIDERS Jun 29 '23 at 17:35

0 Answers0