Skip to content

Instantly share code, notes, and snippets.

@nhudinhtuan
Last active April 3, 2020 02:41
Show Gist options
  • Save nhudinhtuan/8afba48252298c88a39f0ad20c84a683 to your computer and use it in GitHub Desktop.
Save nhudinhtuan/8afba48252298c88a39f0ad20c84a683 to your computer and use it in GitHub Desktop.
Tree traversal - Inorder
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# recursive version
def inorder_traversal_recursive(root):
result = []
def recur(node):
# base case
if not node:
return
# traverse the left subtree
recur(node.left)
# visit node
result.append(node.val)
# traverse the right subtree
recur(node.right)
recur(root)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment