Easy || 0 ms || 100% || Fully Explained (Java, C++, Python, Python3) || Recursive || Iterative
Java Solution (Iterative Approach Using Stack):\nRuntime: 1 ms, faster than 93.68% of Java online submissions for Binary Tree Preorder Traversal.\nMemory Usage:
# **Java Solution (Iterative Approach Using Stack):**\nRuntime: 1 ms, faster than 93.68% of Java online submissions for Binary Tree Preorder Traversal.\nMemory Usage: 41.3 MB, less than 84.60% of Java online submissions for Binary Tree Preorder Traversal.\n```\nclass Solution {\n    public List<Integer> preorderTraversal(TreeNode root) {\n        // Create an array list to store the solution result...\n        List<Integer> sol = new ArrayList<>();\n        // Return the solution answer if the tree is empty...\n        if(root == null) return sol;\n        // Create an empty stack and push the root node...\n        Stack<TreeNode> bag = new Stack<>();\n        bag.push(root);\n        // Loop till stack is empty...\n        while(!bag.isEmpty()){\n            // Pop a node from the stack...\n            TreeNode node = bag.pop();\n            sol.add(node.val);\n            // Push the right child of the popped node into the stack...\n            if(node.right != null) bag.push(node.right);\n            // Push the left child of the popped node into the stack...\n            if(node.left != null) bag.push(node.left);\n        }\n        return sol;     // Return the solution list...\n    }\n}\n```\n\n# **C++ Solution (Recursive Approach):**\nRuntime: 0 ms, faster than 100.00% of C++ online submissions for Binary Tree Preorder Traversal.\nMemory Usage: 8.5 MB, less than 42.50% of C++ online submissions for Binary Tree Preorder Traversal.\n```\nclass Solution {\npublic:\n    vector<int> sol;\n    void preorder(TreeNode* node){\n        if(!node) return;\n        sol.push_back(node->val);\n        preorder(node->left);\n        preorder(node->right);\n    }\n    vector<int> preorderTraversal(TreeNode* root) {\n        preorder(root);\n        return sol;\n    }\n};\n```\n\n# **Python/Python3 Solution (Iterative Approach Using Stack):**\n```\nclass Solution(object):\n    def preorderTraversal(self, root):\n        # Create an empty stack and push the root node...\n        bag = [root]\n        # Create an array list to store the solution result...\n        sol = []\n        # Loop till stack is empty...\n        while bag:\n            # Pop a node from the stack...\n            node = bag.pop()\n            if node:\n                sol.append(node.val)\n                # Append the right child of the popped node into the stack\n                bag.append(node.right)\n                # Push the left child of the popped node into the stack\n                bag.append(node.left)\n        return sol      # Return the solution list...\n```\n**I am working hard for you guys...\nPlease upvote if you found any help with this code...**
['Stack', 'Recursion', 'C', 'Python', 'Java', 'Python3']