Python 3 | Recursive and Iterative Solutions + Visual Explanation
Here is the visual explanation for the iterative version.\nI\'m waiting for your comments\uD83D\uDCA1\nUpvote if you like this post!\n\n\n\n\n\n\n# Iterative Ve
Here is the visual explanation for the iterative version.\nI\'m waiting for your comments\uD83D\uDCA1\n**Upvote** if you like this post!\n\n![return (1).png](https://assets.leetcode.com/users/images/c8ae62f3-3b86-4713-b7a1-5f0c37a35f4f_1673265765.4591982.png)\n\n\n\n\n# Iterative Version\n```\n    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n        self.res, stack = [], [root]\n\n        while stack:\n            n = stack.pop()\n            if n:\n                pre_list.append(n.val)\n                stack.append(n.right)\n                stack.append(n.left)\n\n        return self.res\n```\n\n# Recursive Version\n```\n    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n        self.res = []\n\n        def preorder(curr):\n            if curr:\n                self.res.append(curr.val)\n                preorder(curr.left)\n                preorder(curr.right)\n        \n        preorder(root)\n        return self.res\n```\n\n\n**Upvote** if you like this post!
['Python3']