Mixed recursions:
We have taken simple example above, There can examples of recursions also which is neither a head recursion nor a tail recursion. Plus, there can be multiple recursive calls to the function within a function. For example Consider the recursive code to traverse a tree in in-order:
The recursive call is made at both, the head and tail. such example are difficult to convert to iterative version, because they do not map to a single loop.
8 Comments
[…] I have written in my earlier posts also, Tree problems are better solved using recursion (But recursion comes with its side effect). Finding the Mirror can also be visualized […]
[…] function to reverse the linked list is recursive, but as I said earlier that recursion comes with its costs. so its always better to recursive functions because of time and space […]
[…] Note, that Recursion comes with its own cost. to learn more, see this post… […]
[…] is a classical example of both head recursion and tail recursion happening in the same function. You may also want to see my previous post on ‘Recursive […]
[…] printing the remainders in reverse order (Tail Recursion). 00 Read more from C, C++ Recursion Click here to cancel […]
[…] is an example of Head Recursion. In Head recursion the recursive function is called first and then perform the […]
Best explanation so far found on internet
thanks..