def postorder_optimised(preorder: list, length: int) -> list:
    """
    Use a static index pre_index to iterate over the list items one by one
    when the loop returns, store the root item in list.
    Time Complexity: O(n)
    Space Complexity: O(n)
    """
    min_val, max_val, pre_index = -1000_000, 1000_000, 0

    def postorder_helper(min_val: int, max_val: int) -> None:
        nonlocal preorder, length, pre_index, res

        if (
            pre_index >= length
            or preorder[pre_index] < min_val
            or preorder[pre_index] > max_val
        ):
            return None

        root = preorder[pre_index]
        pre_index += 1
        postorder_helper(min_val, root)
        postorder_helper(root, max_val)
        res.append(root)

    res = []
    postorder_helper(min_val, max_val)
    return res