I figured out that it was StackOverflowError, can someone please help and suggest some way to implement the algorithm in space efficient manner.
@code_master01 : StackOverflowError happens when you use recursion and number of times your function is recursing is a little large . It is not necessarily related to space usage . The same code may work perfectly with same space usage if implemented in iterative fashion . I have also experienced StackOverflowError in past for recursive functions and an iterative implementation of same logic has been accepted .
@vineetpaliwal what modifications do u suggest exactly? because i m doing a dfs using recursion, i wonder whether changing it to iterative version (which explicitly uses stack) would be helpful.
You are right, just use stack and it will work. FYI, if you were using c++ you could stick with your recursive dfs, but java has it’s limits.
yes java supports lesser recursive calls.
most recursive sol give tle