In DFS traversal of a binary tree, we access nodes in three different orders: preorder, postorder, and inorder. Now there is another traversal that can access nodes in level-by-level order. This is called level order traversal or breadth-first search traversal. In the short form, we also call it BFS traversal.
If we observe, a binary tree is organized into different levels where root node is at the topmost level. So one idea is to traverse tree level by level i.e. we start from the root node (level 0), then process all nodes at the first level, second level, and so on. Here we explore all nodes at the current level before moving on to nodes at the next level.
Let's observe the order of nodes in level order traversal: We first process the root node at level 0, and then we process the left and right child at level 1 (left to right order). Similarly, at the second level, we first process children of the left child of the root then process children of the right child. This process goes on for all levels.
Let's observe another pattern: If we first process a node x at any given level, then the children of node x will be processed first at the next level. From another perspective: If node x comes before node y at the same level, then at the next level, we will process the children of node x before processing the children of node y. This is the First In First Out (FIFO) order of processing. So we can use a queue data structure to simulate the level order or BFS traversal.
Step 1: We create an empty queue treeQueue and initialize it with the root node.
Step 2: Now we run a loop until the treeQueue is empty. Inside the loop, we declare a variable currNode to keep track of the current node during the traversal.
Step 3: After processing the rightmost node at the last level, there are no more nodes inside the queue to process. We exit the loop, and the level order traversal is complete.
void levelOrderTraversal(TreeNode root)
{
if (root == NULL)
return
queue<TreeNode> treeQueue
treeQueue.enqueue(root)
while (treeQueue.empty() == false)
{
TreeNode currNode = treeQueue.dequeue()
process(currNode->data)
if (currNode->left != NULL)
treeQueue.enqueue(currNode->left)
if (currNode->right != NULL)
treeQueue.enqueue(currNode->right)
}
}
Suppose n number of nodes are given in the input.
Space complexity is equal to the queue size. We process nodes level by level, so the max queue size depends on the level with the maximum number of nodes or max-width of the binary tree. If the maximum width of the binary tree is w, then space complexity = O(w). The idea is simple: w depends on the structure of a given binary tree. How? Let’s think!
Worst case: When tree is balanced
When tree is balanced, the last level will have maximum width or maximum number of nodes, which will be 2^h (where h is the height of the tree). For balanced tree, h = logn, size of the queue = O(2^h) = O(2^(log n)) = O(n). Space complexity = O(n).
Best case: When tree is skewed
In such case, every level will have one node. So at any point, there will be at most one node in the queue. So size of the queue = O(1). Space complexity = O(1).
If you have any queries or feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy coding, Enjoy algorithms!