def preorder(root):
    """Preorder depth-first traverse a binary tree."""
    ans = []
    stack = [root]
    while stack:
        node = stack.pop()
        if node:
            ans.append(node.val)
            stack.extend([node.right, node.left])
    return ans