time complexity on this is a class of node def initself val leftnone rightnone self val val self left left self right right def preorderroot recursive implementation if root is none return if res is none res res appendroot val preorderrecroot left res preorderrecroot right res return res this is a class of node function to preorder recursive implementation
class Node:
    

    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def preorder(root):
    
    res = []
    if not root:
        return res
    stack = []
    stack.append(root)
    while stack:
        root = stack.pop()
        res.append(root.val)
        if root.right:
            stack.append(root.right)
        if root.left:
            stack.append(root.left)
    return res

def preorder_rec(root, res=None):
    
    if root is None:
        return []
    if res is None:
        res = []
    res.append(root.val)
    preorder_rec(root.left, res)
    preorder_rec(root.right, res)
    return res