Last active
April 2, 2020 07:49
-
-
Save nhudinhtuan/e5008952cf99576c80a9b5904d0c855f to your computer and use it in GitHub Desktop.
Tree traversal - Inorder iterative implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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