Skip to content

recursion vs while loop

goungoun edited this page Oct 12, 2024 · 4 revisions

Choosing recursion solves certain problems much easier than using a stack or a queue (using deque).

Here is an example:
Problem: https://leetcode.com/problems/symmetric-tree/description/
Solution: https://github.com/goungoun/leetcode/blob/main/Tree/symetric_tree.py

But, in some cases, Top-down approach performs better because it solves related problems without solving everything like the bottom up approach.

For example, C(5,4)

i) bottom up
1
1 1
1 2 1
1 3 3 1 
1 4 6 4 1
1 5 10 10 5 -

ii) Top Down
1
1 1
- 2 1
- - 3 1
- - - 4 1
- - - - 5 -

ref: Dynamic Programming by Meenakshi

Clone this wiki locally