Identifying the level order column traversal pattern


The level order traversal over columns 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 column for all columns in the tree. Some problems may require further aggregating the aggregates for each column 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 column traversal technique.

Template:

Given a binary tree, find the aggregate value of a function f over all nodes in a column for all the columns in the tree. Further aggregate the aggregates for all the columns into a single value using a function g. The processing of a node may require some shared state variables.

Example

Liking the course? Check our discounted plans to continue learning.