Level order Traversal of a rooted Tree can be done by starting from root and…
2015
Level order Traversal of a rooted Tree can be done by starting from root and performing :
- A.
Breadth First Search
- B.
Depth First Search
- C.
Root Search
- D.
Deep Search
Attempted by 860 students.
Show answer & explanation
Correct answer: A
Core idea: Level-order traversal visits nodes level by level starting from the root. Implement it using breadth-first search (BFS) with a queue (FIFO).
Algorithm (queue-based):
Enqueue the root node (if it exists).
While the queue is not empty:
Dequeue the front node and visit it; then enqueue all of its children in order (typically left to right).
Pseudo-code: queue = new Queue(); if (root != null) queue.enqueue(root); while (!queue.isEmpty()) { node = queue.dequeue(); visit(node); for each child of node: queue.enqueue(child); }
Time complexity: O(n), where n is the number of nodes (each node is enqueued and dequeued once).
Space complexity: O(n) in the worst case (the queue may hold an entire level of the tree, which can be proportional to n).
A video solution is available for this question — log in and enroll to watch it.