# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
to_ret = []
def preorder(root):
if not root:
return
to_ret.append(root.val)
preorder(root.left)
preorder(root.right)
preorder(root)
return to_ret
If the root node does not exist, return nothing.
Else perform preorder by: