Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
Last active April 2, 2020 07:49
Show Gist options
  • Save nhudinhtuan/e5008952cf99576c80a9b5904d0c855f to your computer and use it in GitHub Desktop.
Save nhudinhtuan/e5008952cf99576c80a9b5904d0c855f to your computer and use it in GitHub Desktop.
Tree traversal - Inorder iterative implementation
def inorder_traversal_iterating(root):
result = []
# using stack
stack = []
current = root
while stack or current:
if current:
# traverse the left subtree
stack.append(current)
current = current.left
continue
# visit node
current = stack.pop()
result.append(current.val)
# traverse the right subtree
current = current.right
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment