Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

But it is possible to get a stack overflow error when using recursion to implement tree traversal if the depth of the tree is too large or if the recursion is not properly managed.

Implementing tree traversal iteratively can help avoid that because it does not rely on recursive function calls that can build up on the call stack. Instead, an iterative approach uses an explicit data structure (such as a stack or queue).



Well, you could run out of memory implementing a stack as an array too. It all depends on what the limits are.


I kind of like that because if your tree depth is that large you're throwing away the benefits of a tree generally and that is an indication you should be doing something different.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: