class TreeNode:
    """Store one integer and its optional child references.

    Attributes:
        value: Integer stored at this node.
        left: Root of the left subtree, or None.
        right: Root of the right subtree, or None.
    """

    def __init__(
        self,
        value: int,
        left: "TreeNode | None" = None,
        right: "TreeNode | None" = None,
    ) -> None:
        """Initialize a node.

        Args:
            value: Integer stored at this node.
            left: Optional left child.
            right: Optional right child.
        """
        self.value = value
        self.left = left
        self.right = right


def append_inorder(node: TreeNode | None, result: list[int]) -> None:
    """Append a subtree's values in inorder.

    Args:
        node: Root of a finite binary tree, or None.
        result: Destination list to extend.
    """
    if node is None:
        return
    append_inorder(node.left, result)
    result.append(node.value)
    append_inorder(node.right, result)


def inorder_recursive(root: TreeNode | None) -> list[int]:
    """Return an inorder traversal using recursive calls.

    Args:
        root: Root of a finite binary tree, or None.

    Returns:
        Values in left subtree, root, right subtree order.
    """
    result: list[int] = []
    append_inorder(root, result)
    return result


def maximum_depth(root: TreeNode | None) -> int:
    """Return the greatest root-to-leaf node count.

    Args:
        root: Root of a finite binary tree, or None.

    Returns:
        The maximum depth, with zero for the empty tree.
    """
    if root is None:
        return 0
    return 1 + max(maximum_depth(root.left), maximum_depth(root.right))


tree = TreeNode(8, TreeNode(3, TreeNode(1), TreeNode(6)), TreeNode(11))
print("Recursive inorder:", inorder_recursive(tree))
print("Maximum depth:", maximum_depth(tree))

def inorder_iterative(root: TreeNode | None) -> list[int]:
    """Return an inorder traversal using an explicit stack.

    Args:
        root: Root of a finite binary tree, or None.

    Returns:
        Values in left subtree, root, right subtree order.
    """
    result: list[int] = []
    stack: list[TreeNode] = []
    current = root
    while current is not None or stack:
        while current is not None:
            stack.append(current)
            current = current.left
        current = stack.pop()
        result.append(current.value)
        current = current.right
    return result


stack: list[TreeNode] = []
current = tree
while current is not None or stack:
    while current is not None:
        stack.append(current)
        print("Push", current.value, "stack", [node.value for node in stack])
        current = current.left
    current = stack.pop()
    print("Visit", current.value, "stack", [node.value for node in stack])
    current = current.right
print("Iterative inorder:", inorder_iterative(tree))

assert inorder_recursive(None) == []
assert inorder_iterative(None) == []
assert maximum_depth(None) == 0
single = TreeNode(5)
assert inorder_iterative(single) == [5]
assert maximum_depth(single) == 1
chain = TreeNode(2, None, TreeNode(4, None, TreeNode(9)))
assert inorder_recursive(chain) == inorder_iterative(chain)
assert maximum_depth(chain) == 3
unordered = TreeNode(2, TreeNode(7), TreeNode(1))
assert inorder_iterative(unordered) == [7, 2, 1]
print("The empty, singleton, chain, and unordered cases pass.")
