Has Path Sum (binary tree)
June 11, 2012
Basic Tree Traversals (Preorder, Inorder, Postorder)
June 11, 2012

Understanding Recursion

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:

traversal_in.JPG

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

  1. […] 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 […]

  2. […] 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 […]

  3. […] Note, that Recursion comes with its own cost. to learn more, see this post… […]

  4. […] 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 […]

  5. […] printing the remainders in reverse order (Tail Recursion). 00    Read more from C, C++ Recursion Click here to cancel […]

  6. […] is an example of Head Recursion. In Head recursion the recursive function is called first and then perform the […]

  7. anonymous says:

    Best explanation so far found on internet

Leave a Reply

Your email address will not be published. Required fields are marked *