I dont understand how are we applying recursion here, I mean we see the smaller problem and then make changes to it to reach to the sol,
But here what is the smaller problem and what are the changes that we are making
Also that index confuses more
I get the solution but not the recursion appraoch
Hey @bhavik911
Simple use this logic
if(stack.isEmpty()){
return ;
}
//pop out all the items from the stack and store it in function stack
int x = stack.pop();
reverse(stack);
//now insert the items into stack in reversed order
//last item popped from the stack
insert_at_bottom(stack, x); }
insert_at_bottom(stack, x)
First pops all stack items and stores the popped item in function call stack using recursion. And when stack becomes empty, pushes new item and all items stored in call stack.
But in this we have to use 2 recursion ,right?
Also this doesnt use helper stack and its time complexity is O(n^2)
Right??
This is not the same as sol given in video?
Question 1 : But in this we have to use 2 recursion ,right?
Answer : Yes
Question 2 : Also this doesnt use helper stack its time complexity is O(n^2)
Answer : Yes its Right
Question 3: This is not the same as sol given in video?
Answer its different
What is call stack?
Can you explain it a bit more
Java call stack contains stack frames . Stack frame contains the information regarding method and its variables(variables local to the method). A stack frame is added to the call stack when the execution enters the method. And the stack frame is removed, from the call stack, for a method, if the execution is done in the method.
Is there any relation between call stack and stack data structure
call stack bhi stack data structure ki tarah work krta hai
I am trying to understand recursion in Java by visualizing it. I have gone through some tutorials on youtube and using the below example from one of them
public class Main {
public static void main(String []args) {
reduceByOne(10);
}
public static void reduceByOne(int n) {
System.out.println("Before "+n);
if(n >= 0) {
reduceByOne(n-1);
System.out.println("Inside "+n);
}
System.out.println("After "+n);
}
}
From what I have understood so far, every call to reduceByOne() would be placed in the execution stack. Something like
So it is just the execution stack made as were dicussed in earlier videos?