Tree Traversal

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

What you'll learn

tree_traversal.py

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)

Step by step

  1. Python starts building the class TreeNode. Its methods are stored, but none of them run yet.
  2. Python reads the class body: everything indented under class TreeNode belongs to it.
  3. The method __init__ is stored in TreeNode. It runs only when it is called on an object.
  4. The class TreeNode is now defined, ready to make objects from.
  5. Python defines the function inorder(). Its body does not run yet: it runs each time the function is called.
  6. Python calls __init__(), with self = {}, value = 1, left = null and right = null. It gets its own names.
  7. A new key value with the value 1 is added to the TreeNode that self points to.
  8. A new key left with the value null is added to the TreeNode that self points to.
  9. A new key right with the value null is added to the TreeNode that self points to.
  10. __init__() has finished. Python calls __init__(), with self = {}, value = 3, left = null and right = null. It gets its own names.
  11. A new key value with the value 3 is added to the TreeNode that self points to.
  12. A new key left with the value null is added to the TreeNode that self points to.
  13. A new key right with the value null is added to the TreeNode that self points to.
  14. __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.
  15. A new key value with the value 2 is added to the TreeNode that self points to.
  16. A new key left with the value a TreeNode is added to the TreeNode that self points to.
  17. A new key right with the value a TreeNode is added to the TreeNode that self points to.
  18. __init__() has finished, so its names are gone. The value it returned, {value: 2, left: a TreeNode, right: a TreeNode}, is stored in root.
  19. result is created and points to a new list: [].
  20. Start at the root, 2.
  21. node is None is false, so the block under it is skipped.
  22. Before visiting 2, go left: inorder(node 1).
  23. node is None is false, so the block under it is skipped.
  24. Python calls inorder() #3, with node = null and out = []. It gets its own names.
  25. node is None is true, so the block under it runs.
  26. An empty subtree (None): nothing to do, return.
  27. inorder() #3 has finished, so its names are gone.
  28. Node 1's left side is done, so visit 1.
  29. Python calls inorder() #3, with node = null and out = [1]. It gets its own names.
  30. node is None is true, so the block under it runs.
  31. The function returns.
  32. inorder() #3 has finished, so its names are gone.
  33. inorder() #2 has finished, so its names are gone.
  34. Back at the root: its left side is done, visit 2.
  35. Python calls inorder() #2, with node = {value: 3, left: null, right: null} and out = [1, 2]. It gets its own names.
  36. node is None is false, so the block under it is skipped.
  37. Python calls inorder() #3, with node = null and out = [1, 2]. It gets its own names.
  38. node is None is true, so the block under it runs.
  39. The function returns.
  40. inorder() #3 has finished, so its names are gone.
  41. 3 is added to the end of the list that result and out point to.
  42. Python calls inorder() #3, with node = null and out = [1, 2, 3]. It gets its own names.
  43. node is None is true, so the block under it runs.
  44. The function returns.
  45. inorder() #3 has finished, so its names are gone.
  46. inorder() #2 has finished, so its names are gone.
  47. inorder() has finished, so its names are gone.
  48. [1, 2, 3]: left, root, right.

Common mistakes

Best practices

Key takeaways

Check yourself

Pre-order of this tree?

Answer: [2, 1, 3]. Node first.

Base case of the recursion?

Answer: node is None

·

More in Trees and Heaps