B2.4.4–5 · HL recursion

Binary-tree traversal · HL application

Python · step by step

Binary-tree traversal · HL application

An in-order traversal visits the left subtree, the node, then the right subtree.

Select Play or Next line to follow Python execution.

Python implementation

The highlight marks the next line to execute. Values show the state before that line.

visits = []def tree_traversal(node):    if node is None:        return    tree_traversal(node["left"])    visits.append(node["value"])    tree_traversal(node["right"])

B2.4.4 application, not a B2.4.5 branching-code requirement. Each dictionary represents a node; None means no child. This particular tree is a binary search tree, so in-order traversal produces sorted output. Each node is visited once: O(n) time, O(h) auxiliary stack space for tree height h, excluding the output log.

Keep exploring

Return to the recursion lesson →

All B2.4 animations →

All practice & resources →