Why is that recursion is only used for trying all the possibilities or combinations?

Is it because recursion can go in every possible state, and if not found an adequate solution can traverse back (backtracking)?

@raghav6 Yes you are correct. But it is not that recursion is only used. Example: In generating all subsequences of string or generating all subsets of a given set, both itertative(using bitmasking) and recursive approach can be followed. We use recursion only when it is very complex to write iterative code. For example, tree traversal techniques like preorder, postorder can be made both iterative and recursive. But usually we use recursive because of its simplicity. The problem is solved by repetitively breaking it into smaller problem which are similar in nature to the original problem. There may be many such problems where iterative approach is very difficult to think but you can easily get the recursive approach.

1 Like

Thank you @pratyush63. Resolved the doubt! :grinning: Nicely explained :slight_smile: