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.
- Tree: Preorder Traversal
- Discussions
Tree: Preorder Traversal
Tree: Preorder Traversal
Sort by
recency
|
9 Discussions
|
Please Login in order to post a comment
Java O(n)
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).function preOrder(root) {
Python
Recursive Method- TC is O(n log n) and SC is O(1)
def preOrder(root):
Iterative Method- TC is O(n) and SC is O(1)
def preOrder(root):