Solution Review: Tree to List Conversion using Recursion

Let’s take a detailed look at the previous challenge’s solution.

Solution

The tree to the list conversion is done recursively. At each node, we must assume that the treeToListRec() function will do its job for the left child and right child. Then we combine the result of the left child and right child traversal. We need a head and tail pointer of the left list and right list to combine them with the current node. In the process of integration, the current node is added to the tail of the left list and the current node is added to the head to the right list. The head of the left list becomes the head of the newly-formed list and the tail of the right list becomes the tail of the newly created list.

Solution code

Get hands-on with 1400+ tech skills courses.