Loading…
Loading…
Breadth-first: visits the tree one level at a time, left to right, using a queue.
Level-order: visit the tree row by row (BFS).
Output: []
1void levelOrder(Node root) {2 Queue<Node> q = new LinkedList<>(); q.add(root);3 while (!q.isEmpty()) {4 Node node = q.poll();5 out.add(node.val);6 if (node.left != null) q.add(node.left);7 if (node.right != null) q.add(node.right);8 }9}