Question

What are examples of problems that can be more easily solved by using recursion in Python?

What are examples of problems that can be more easily solved by using recursion in Python?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
inorder, preorder, postorder traversals of tree is the best examples of problems that can be more easily solved by using recursion.

inorder:
--------------
def inorder(tree):
    if(tree!=None):
        inorder(tree.left)
        print(tree.data)
        inorder(tree.right)



preorder:
-------------
def preorder(tree):
    if(tree!=None):
        print(tree.data)
        preorder(tree.left)
        preorder(tree.right)


postorder:
---------------
def postorder(tree):
    if(tree!=None):
        postorder(tree.left)
        postorder(tree.right)
        print(tree.data)


But these algorithms are most helpful to understand if you know the concepts of binary trees.

Easy example to explain is fibonacci series problem

fibonacci:
------------
def fibonacci(n):
    if(n == 0):
        return 0
    elif(n == 1):
        return 1
    else:
        return fibonacci(n-1)+fibonacci(n-2)

So, base cases are when n==0 and n==1
and recursive case is when n>1

below if have doubts Pleas e comment you any

Add a comment
Know the answer?
Add Answer to:
What are examples of problems that can be more easily solved by using recursion in Python?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT