Visit every node of a binary tree in order: left subtree, the node, right subtree. Recursion does the bookkeeping.
Python · intermediate · 8 min · 48 steps · free
class TreeNode:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def inorder(node, out):
if node is None:
return
inorder(node.left, out)
out.append(node.value)
inorder(node.right, out)
root = TreeNode(2, TreeNode(1), TreeNode(3))
result = []
inorder(root, result)
print(result)TreeNode. Its methods are stored, but none of them run yet.class TreeNode belongs to it.__init__ is stored in TreeNode. It runs only when it is called on an object.TreeNode is now defined, ready to make objects from.inorder(). Its body does not run yet: it runs each time the function is called.__init__(), with self = {}, value = 1, left = null and right = null. It gets its own names.value with the value 1 is added to the TreeNode that self points to.left with the value null is added to the TreeNode that self points to.right with the value null is added to the TreeNode that self points to.__init__() has finished. Python calls __init__(), with self = {}, value = 3, left = null and right = null. It gets its own names.value with the value 3 is added to the TreeNode that self points to.left with the value null is added to the TreeNode that self points to.right with the value null is added to the TreeNode that self points to.__init__() has finished. Python calls __init__(), with self = {}, value = 2, left = {value: 1, left: null, right: null} and right = {value: 3, left: null, right: null}. It gets its own names.value with the value 2 is added to the TreeNode that self points to.left with the value a TreeNode is added to the TreeNode that self points to.right with the value a TreeNode is added to the TreeNode that self points to.__init__() has finished, so its names are gone. The value it returned, {value: 2, left: a TreeNode, right: a TreeNode}, is stored in root.result is created and points to a new list: [].node is None is false, so the block under it is skipped.node is None is false, so the block under it is skipped.inorder() #3, with node = null and out = []. It gets its own names.node is None is true, so the block under it runs.inorder() #3 has finished, so its names are gone.inorder() #3, with node = null and out = [1]. It gets its own names.node is None is true, so the block under it runs.inorder() #3 has finished, so its names are gone.inorder() #2 has finished, so its names are gone.inorder() #2, with node = {value: 3, left: null, right: null} and out = [1, 2]. It gets its own names.node is None is false, so the block under it is skipped.inorder() #3, with node = null and out = [1, 2]. It gets its own names.node is None is true, so the block under it runs.inorder() #3 has finished, so its names are gone.result and out point to.inorder() #3, with node = null and out = [1, 2, 3]. It gets its own names.node is None is true, so the block under it runs.inorder() #3 has finished, so its names are gone.inorder() #2 has finished, so its names are gone.inorder() has finished, so its names are gone.Answer: [2, 1, 3]. Node first.
Answer: node is None
← Two Sum with a Hash Map · Binary Search Trees →