We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
C++, iterative with a stack instead of recursive. We visit each node, then push its right and then left (in reverse order) so they are visited in the correct order as we later pop the stack.
The Hackerrank solution code doesn't include <vector> which you kinda need, so I hacked a way to get it included. Here's the full text of my solution, verbatim (it intentionally ends with an open brace).
Tree: Preorder Traversal
You are viewing a single comment's thread. Return to all comments →
C++, iterative with a stack instead of recursive. We visit each node, then push its right and then left (in reverse order) so they are visited in the correct order as we later pop the stack.
The Hackerrank solution code doesn't include
<vector>
which you kinda need, so I hacked a way to get it included. Here's the full text of my solution, verbatim (it intentionally ends with an open brace).