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.
defpreOrder(root):# it would be useful to have a stack here to keep track of the next node to visit# if I start going down a left child, the next node to visit would be the right child# I also will need to set my current pointer to the current node# and create a result arraycur=rootstack=[]# my loop is finished when# cur is None - OR# stack is None - stack is only None at beginning or endwhilecurorstack:ifcur:print(cur.info,end=' ')stack.append(cur.right)cur=cur.leftelse:cur=stack.pop()
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 →
Python3