I’m stuck at finding a solution. C#, .NET 4.0, VS2010
I can easily write a recursive one, but can’t for the life of me figure out something that won’t overflow the stack if the tree is arbitrarily large.
This is a binary tree question, and i am trying to write a
public IEnumerable<T> Values()
method.
Here is the full code in case you are interested: http://pastebin.com/xr2f3y7g
Obviously, the version currently in there doesn’t work. I probably should mention that I am a newbie in C#, transitioning from C++.
Here is a method for inorder traversal, that uses explicit stack. The stack is created on the heap, so it can be much larger, than the stack the processor uses.
If you can’t use a stack and your nodes happen to have parent pointers, you can try solutions from this question