Python | Recursive & Iterative | Simple Solutions
Recursive Solution\n\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val
### Recursive Solution\n```\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def traversal(self, root, preorder):\n        if root:\n            preorder.append(root.val)\n            self.traversal(root.left, preorder)\n            self.traversal(root.right, preorder)\n            \n    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n        preorder = []\n        self.traversal(root, preorder)\n        return preorder\n```\n\n### Iterative Solution\n```\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n        preorder = []\n        stack = [root]\n        while stack:\n            root = stack.pop()\n            if root:\n                preorder.append(root.val)\n                stack.append(root.right)\n                stack.append(root.left)\n        return preorder\n```\n\n***If you liked the above solution then please upvote!***
['Python']