Preview:
def dfs(root):
    if not root:
        return []

    stack = [root]
    traversal = []

    while stack:
        node = stack.pop()
        traversal.append(node.value)

        if node.right:
            stack.append(node.right)
        if node.left:
            stack.append(node.left)

    return traversal
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter