 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Conversion of Looping to Recursive Solution

By : Nora
Date : July 29 2020, 12:00 PM
Does that help I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the returning list(List of tuples). Any help will be highly appreciated. , I don't see where recursion would offer significant advantages. code :
``````def pythagoreanTriplets(limit: Int): List[(Int, Int, Int)] =
for {
m <- (2 to limit/2).toList
n <- 1 until m
c =  m*m + n*n if c <= limit
} yield (m*m - n*n, 2*m*n, c)
``````

## How to convert a recursive solution to bottom up or top down solution?

By : Floor Leemans
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I'm a bit too lazy to think the problem through, but I'll give you an approach to memoization that often enough works.
Instead of recursing directly, introduce mutual recursion.
code :
``````std::string reduce(std::string const &s)
{
// ...
string s1 = reduce_memo(s.substr(0, k));
string s2 = reduce_memo(s.substr(k));
// ...
}
``````
``````// static is incredibly ugly, but I'll use it here for simplicity
static std::unordered_map<std::string, std::string> memo;

std::string reduce_memo(std::string const &s)
{
try {
return memo.at(s);
} except (std::out_of_range const &) {
std::string r = reduce(s);
memo[s] = r;
return r;
}
}
``````

## Why is this MinDepth level solution so slow compared to recursive solution?

By : user2201330
Date : March 29 2020, 07:55 AM
hope this fix your issue I wouldn't put a whole lot of weight on the LeetCode percentages. When I submit your recursive solution, it shows it beats 34%. LeetCode also shows the exact same example code for both 100% and 34% segments. One can only guess what exactly their test cases are. All implementations I submitted run in 1 ms, so chances are, all 41 of their test cases are very small trees such that performance differences are totally negligible. You also don't know what kind of tree structures dominate the example cases--they could all be more or less worst-case time complexity, in which case BFS has little or no advantage over DFS.
With that in mind, let's benchmark your code on large test cases to see if we can gain some understanding that can't be obtained on the black-box testing environment that LeetCode offers.
code :
``````public int minDepth(TreeNode root) {
ArrayDeque<Pair<TreeNode, Integer>> q = new ArrayDeque<>();
q.offer(new Pair(root, 1));

while (!q.isEmpty()) {
Pair<TreeNode, Integer> curr = q.poll();

if (curr.first != null) {
if (curr.first.left == null && curr.first.right == null) {
return curr.second;
}

q.offer(new Pair(curr.first.left, curr.second + 1));
q.offer(new Pair(curr.first.right, curr.second + 1));
}
}

return 0;
}
``````
``````long BFSTotal = 0;
long BFS2Total = 0;
long DFSTotal = 0;

for (int i = 0; i < 10000; i++) {
TreeNode root = randomTree(10000);

long start = System.currentTimeMillis();
minDepthDFS(root);
DFSTotal += System.currentTimeMillis() - start;

start = System.currentTimeMillis();
minDepthBFS(root);
BFSTotal += System.currentTimeMillis() - start;

start = System.currentTimeMillis();
minDepthBFS2(root);
BFS2Total += System.currentTimeMillis() - start;
}

System.out.println("BFS: " + BFSTotal);
System.out.println("BFS2: " + BFS2Total);
System.out.println("DFS: " + DFSTotal);
``````
``````BFS: 1906
BFS2: 5484
DFS: 3351

BFS: 1709
BFS2: 6101
DFS: 3773

BFS: 1527
BFS2: 5567
DFS: 3856
``````

## How do I easily convert an iterative solution to a recursive solution?

By : user2367006
Date : March 29 2020, 07:55 AM
Hope that helps Virtually every recursive function can be designed around a "base case" and a "recursive case".
The base case might be a single node, with no children: this yields a trivial doubly-linked list of one element. In the recursive case, you need to build the list for the left child (if any), then the current node, and then finally the right child.

## Building Maximum Subarray Dynamic Programming solution from Recursive Solution

By : user3148922
Date : March 29 2020, 07:55 AM
wish helps you Your arr works like a global variable so we can only use it as parameter. Your dp is an auxiliar variable, a pivot for next iteration. Your max is a variable objective. All working is done in:
code :
``````pivot = Math.max(0, arr[i] + pivot);
if (pivot >= max){
max = pivot;
}
``````
``````private static int resolveR(int i, int max, int pivot, int[] arr){
//initialize
if (i == arr.length){
return max;
} else {
pivot = Math.max(0, arr[i] + pivot);
if (pivot >= max){
max = pivot;
}
return resolveR(i+1, max, pivot, arr);
}
}
``````

## Converting an iterative solution to a recursive solution

By : alperg9
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , It's often the case that recursive functions are easier to reason about and shorter. Typically you'll start with a base case. Here you can imagine what your function should yield with an empty string. Probably ''.
Next if your first character is not an X you just yield that first character plus the result of recursively calling the rest. If it is and X then you yield both 1+recursive call and 0+recursive call. Something like:
code :
``````def combos(s):
if len(s) == 0:
yield  ''
return

for combo in combos(tail): 