Solution
C++ []\nclass Solution {\npublic:\n    vector<int> preorderTraversal(TreeNode* root) {\n        vector<int> preorder;\n        stack<TreeNode*> stack;\n        
```C++ []\nclass Solution {\npublic:\n    vector<int> preorderTraversal(TreeNode* root) {\n        vector<int> preorder;\n        stack<TreeNode*> stack;\n        if (root == NULL)\n            return preorder;\n        stack.push(root);\n        while(!stack.empty()) {\n            TreeNode* curr = stack.top();\n            stack.pop();\n            preorder.push_back(curr->val);\n            if (curr->right != NULL)\n                stack.push(curr->right);\n            if (curr->left != NULL)\n                stack.push(curr->left);\n        }\n        return preorder;\n    }\n};\n```\n\n```Python3 []\nclass Solution:\n    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n        head = root\n        stack = []\n        res = []\n\n        while head or stack:\n            if head:\n                res.append(head.val)\n                if head.right:\n                    stack.append(head.right)\n                head = head.left\n            else:\n                head = stack.pop()\n\n        return res        \n```\n\n```Java []\nclass Solution {\n    List<Integer> preorderTraverse(TreeNode root,List<Integer> list) {\n\n        if(root==null)\n            return list;\n        list.add(root.val);\n        preorderTraverse(root.left,list);\n        preorderTraverse(root.right,list);\n        return list;\n    }\n    public List<Integer> preorderTraversal(TreeNode root) {\n        List<Integer> list = new ArrayList<Integer>();\n        list = preorderTraverse(root,list);\n        return list;\n    }\n}\n```\n
['C++', 'Java', 'Python3']