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.
if root is not None:
print(root.info,end=" ")
preOrder(root.left)
preOrder(root.right)
Iterative Method- TC is O(n) and SC is O(1)
def preOrder(root):
stack= [root]
result = []
if root is None:return []
while stack:
root = stack.pop()
result.append(root.info)
if root.right:
stack.append(root.right)
if root.left :
stack.append(root.left)
print (*result)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Preorder Traversal
You are viewing a single comment's thread. Return to all comments →
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):