these type of questions can be solved using both recurcise and iterative or bfs approach.
in recurcise code to be written is also less and is somewhat easy to implement but the iterative one requires more code writing and some thinking.
so when these type of questions come to us then which approach to prefer.