💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
Intuition\n\n\n\n\n\n\nJavaScript []\n//JavaScript Code\n/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n *     this.val =
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/0eb7f58e-0f9a-4134-a55a-fc68e55f7ab9_1713326839.6583364.jpeg)\n![Screenshot 2024-04-17 073521.png](https://assets.leetcode.com/users/images/32853c17-5f6c-47cb-a391-146018580800_1713326849.2931008.png)\n![Screenshot 2024-04-17 074614.png](https://assets.leetcode.com/users/images/e6142372-808b-484e-9ca3-93d8c623ae2b_1713326883.972275.png)\n![Screenshot 2024-04-17 074623.png](https://assets.leetcode.com/users/images/d42bf151-626c-4615-8da7-bdb83e30057c_1713326893.0237114.png)\n![Screenshot 2024-08-25 122822.png](https://assets.leetcode.com/users/images/a803c727-b872-4c1e-8cf7-55d2fca2b02f_1724659188.121047.png)\n![Screenshot 2024-08-25 122941.png](https://assets.leetcode.com/users/images/c9c6681f-de63-491f-aace-0f8e7f3c766a_1724659195.7623672.png)\n```JavaScript []\n//JavaScript Code\n/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.left = (left===undefined ? null : left)\n *     this.right = (right===undefined ? null : right)\n * }\n */\n/**\n * @param {TreeNode} root\n * @return {number[]}\n */\nvar preorderTraversal = function(root) {\n    const arr = []\n    var preOrder = ((root) => {\n        if(!root)\n            return\n        \n        arr.push(root.val)\n        preOrder(root.left)\n        preOrder(root.right)\n        return\n    })\n    preOrder(root)\n    return arr\n};\n```\n```C++ []\n//C++ Code\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n    vector<int> ans;\n\npublic:\n    void preorder(TreeNode* root, vector<int>& ans) {\n        if (root) {\n            ans.push_back(root->val);\n            preorder(root->left, ans);\n            preorder(root->right, ans);\n        }\n        return;\n    }\n    vector<int> preorderTraversal(TreeNode* root) {\n        preorder(root, ans);\n        return ans;\n    }\n};\n```\n```python3 []\n#Python3 Code\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        res = []\n\n        def _preorder(root):\n            if root:\n                res.append(root.val)\n                _preorder(root.left)\n                _preorder(root.right)\n            return\n        \n        _preorder(root)\n        return res\n```\n```C []\n//C Code\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n */\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nvoid preorder(struct TreeNode* root, int* arr, int* returnSize) {\n    if (root) {\n        arr[(*returnSize)++] = root->val;\n        preorder(root->left, arr, returnSize);\n        preorder(root->right, arr, returnSize);\n    }\n    return;\n}\nint* preorderTraversal(struct TreeNode* root, int* returnSize) {\n    int* arr = (int*)malloc(sizeof(int) * 100);\n    *returnSize = 0;\n    preorder(root, arr, returnSize);\n    return arr;\n    free(arr);\n}\n```\n```Java []\n//Java Code\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = left;\n * this.right = right;\n * }\n * }\n */\nclass Solution {\n    List<Integer> ans = new ArrayList<>();\n\n    private void preorder(TreeNode root) {\n        if (root == null)\n            return;\n        ans.add(root.val);\n        preorder(root.left);\n        preorder(root.right);\n        return;\n    }\n\n    public List<Integer> preorderTraversal(TreeNode root) {\n        preorder(root);\n        return ans;\n    }\n}\n```\n```python []\n#Pytho Code\n# Definition for a binary tree node.\n# class TreeNode(object):\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution(object):\n    def preorderTraversal(self, root):\n        """\n        :type root: TreeNode\n        :rtype: List[int]\n        """\n        res = []\n\n        def _preorder(root):\n            if root:\n                res.append(root.val)\n                _preorder(root.left)\n                _preorder(root.right)\n            return\n        \n        _preorder(root)\n        return res\n```\n```C# []\n//C# Code\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     public int val;\n *     public TreeNode left;\n *     public TreeNode right;\n *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\npublic class Solution {\n    List<int> res = new List<int>();\n    private void preOrder(TreeNode root){\n        if(root == null)\n            return;\n        \n        res.Add(root.val);\n        preOrder(root.left);\n        preOrder(root.right);\n    }\n    public IList<int> PreorderTraversal(TreeNode root) {\n        preOrder(root);\n        return res;\n    }\n}\n```\n---\n1. **Also Check out the Same Questions Like this.**\n- ***LC _ 94 Binary Tree Inorder Traversal*** https://leetcode.com/problems/binary-tree-inorder-traversal/solutions/4328091/faster-lesser-c-python3-java-c-python-explained-beats\n- ***LC _ 145 Binary PostOrder Traversal*** https://leetcode.com/problems/binary-tree-postorder-traversal/solutions/5039124/easiest-c-python3-c-java-c-python-beats-simplified-version\n---\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n      O(n) -- no. of nodes\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n      O(1)\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = left;\n * this.right = right;\n * }\n * }\n */\nclass Solution {\n    List<Integer> ans = new ArrayList<>();\n\n    private void preorder(TreeNode root) {\n        if (root == null)\n            return;\n        ans.add(root.val);\n        preorder(root.left);\n        preorder(root.right);\n        return;\n    }\n\n    public List<Integer> preorderTraversal(TreeNode root) {\n        preorder(root);\n        return ans;\n    }\n}\n```\n## Please upvote if it\'s useful for you ... If it\'s helpful for you.. Then Please upvote my solution that will be a great help for me and motivates me to do better..\n![th.jpeg](https://assets.leetcode.com/users/images/eb04b7d9-9197-4f79-8e23-949e4d802ff6_1713327118.0693405.jpeg)\n
['Stack', 'Depth-First Search', 'C', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']