Why do we need queues or stacks?


  • 0
    C

    I managed to get an accepted solution using a recursive DFS keeping track of the depth and either inserting at the front or back depending on the level. Why would an iterative solution using queues/stacks be better?


  • 0
    P

    Implementing non-recursive solution you avoid stack overflow problem, when the execution stack is full of invocation function itself. It could happen when the binary tree is big enough. Thus non-recursive solution is better than recursive.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.