Identifying the level order traversal pattern
The level order traversal can only solve some specific types of binary tree problems. These are generally easy or medium problems where we need to find the aggregate value of some function f over all nodes in a level for all levels in the tree. Some problems may require further aggregating the aggregates for each level into a single value using some other function g. In some cases, we may also need to maintain some shared state information throughout the traversal, which can be easily done by creating local variables before starting the traversal, as level order traversal is fully iterative and not recursive.
If the problem statement or its solution follows the generic template below, it can be solved using the level order traversal technique.
f over all nodes in a level for all the levels in the tree. Further aggregate the aggregates for all the levels into a single value using a function g. The processing of a node may require some shared state variables.Liking the course? Check our discounted plans to continue learning.