question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
construct-binary-search-tree-from-preorder-traversal | Explaining the O(N) approach with C++ code. Beats 100% solutions. | explaining-the-on-approach-with-c-code-b-6fy6 | The O(NlogN) approch which everybody tries is O(N\*N). Lets say the case where array is sorted in decreasing order, there it will go quadratic. 5, 4, 3, 2, 1\n\ | mehul_02 | NORMAL | 2020-04-20T08:04:18.832794+00:00 | 2020-04-20T08:04:18.832830+00:00 | 7,816 | false | The O(NlogN) approch which everybody tries is O(N\\*N). Lets say the case where array is sorted in decreasing order, there it will go quadratic. 5, 4, 3, 2, 1\n\nWe understand the given O(N) solution by:\n1. It is preorder traversal that is all the nodes in left tree is encountered before the nodes of right so we can g... | 71 | 3 | [] | 12 |
construct-binary-search-tree-from-preorder-traversal | simple recusrsive cpp solution | simple-recusrsive-cpp-solution-by-bitris-enkl | \nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& preorder) {\n if(preorder.size()==0)\n return NULL;\n TreeNode* | bitrish | NORMAL | 2020-09-10T09:38:45.269172+00:00 | 2020-09-10T09:38:45.269203+00:00 | 5,662 | false | ```\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& preorder) {\n if(preorder.size()==0)\n return NULL;\n TreeNode* root=new TreeNode(preorder[0]);\n if(preorder.size()==1)\n return root;\n vector<int>left;\n vector<int>right;\n for(... | 59 | 0 | ['Recursion', 'C', 'C++'] | 8 |
construct-binary-search-tree-from-preorder-traversal | JAVA 3 WAYS TO DO THE PROBLEM! O(N) APPROACH | java-3-ways-to-do-the-problem-on-approac-obh6 | THIS IS A CONTINUATION OF MY PREVIOUS POST!\nWHICH HAD O(N^2) APPROACH!\nhttps://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/disc | naturally_aspirated | NORMAL | 2020-04-21T03:10:47.892374+00:00 | 2020-04-21T04:52:52.594307+00:00 | 6,059 | false | THIS IS A CONTINUATION OF MY PREVIOUS POST!\nWHICH HAD O(N^2) APPROACH!\nhttps://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/discuss/589059/java-easiest-solution-with-clear-explanation-of-logic\n\n\nAS A LOT OF PEOPLE ARE ASKING FOR LINEAR SOLUTONS HERE IT IS!\n\nAPPROACH 1-O(N)\nJAVA-LOW... | 42 | 0 | ['Recursion', 'Iterator', 'Java'] | 3 |
construct-binary-search-tree-from-preorder-traversal | C++ Simple and Clean Recursive Solution, Explained | c-simple-and-clean-recursive-solution-ex-udhq | Idea:\nIn a preorder traversal, we visit the root first.\nSo in each iteration, the first element - preorder[i] - is the root\nthen we find the left and right s | yehudisk | NORMAL | 2021-10-13T07:31:16.909198+00:00 | 2021-10-13T07:31:16.909238+00:00 | 3,632 | false | **Idea:**\nIn a preorder traversal, we visit the root first.\nSo in each iteration, the first element - `preorder[i]` - is the root\nthen we find the left and right subtrees and construct the tree recursively.\nIn the left subtree, the maximum value is the current root.\nIn the right subtree, the maximum value is the p... | 32 | 2 | ['C'] | 3 |
construct-binary-search-tree-from-preorder-traversal | Less than 10 lines C++ beats 100% | less-than-10-lines-c-beats-100-by-zhxb51-fhw7 | Even though the elements between begin and left are not sorted, we can still apply binary search on it. The reason is that comparing with the value of current r | zhxb515 | NORMAL | 2019-07-01T23:29:22.915022+00:00 | 2019-07-01T23:29:22.915065+00:00 | 2,955 | false | Even though the elements between begin and left are not sorted, we can still apply binary search on it. The reason is that comparing with the value of current root element, there is a point that all elements on the left side are smaller than root value, and all elements on the right side are larger.\n\n```\nclass Solut... | 30 | 4 | [] | 3 |
construct-binary-search-tree-from-preorder-traversal | [Python] two O(n) solutions, explained | python-two-on-solutions-explained-by-dba-o5el | We can do in simple way: find index for left part, and for right part and do recursion with time complexity O(n^2) for skewed trees and average O(n log n). Ther | dbabichev | NORMAL | 2021-10-13T07:32:40.122410+00:00 | 2021-10-13T07:32:40.122465+00:00 | 1,389 | false | We can do in simple way: find index for left part, and for right part and do recursion with time complexity `O(n^2)` for skewed trees and average `O(n log n)`. There is smarter solution with `O(n)` time complexity, where we give the function a `bound` (or two bounds - up and down) the maximum number it will handle.\nTh... | 28 | 3 | ['Recursion'] | 0 |
construct-binary-search-tree-from-preorder-traversal | JAVA | Clean Code Solution | O(N log N) Time Complexity | 0 ms time | java-clean-code-solution-on-log-n-time-c-y5cl | class Solution {\n \n\tprivate TreeNode insert(TreeNode root, int val) {\n\t\t\n\t\tif(root == null) return new TreeNode(val);\n\t\telse if(root.val > val) root | anii_agrawal | NORMAL | 2020-05-24T21:24:32.863828+00:00 | 2020-05-25T04:36:16.887071+00:00 | 1,581 | false | class Solution {\n \n\tprivate TreeNode insert(TreeNode root, int val) {\n\t\t\n\t\tif(root == null) return new TreeNode(val);\n\t\telse if(root.val > val) root.left = insert(root.left, val);\n\t\telse root.right = insert(root.right, val);\n\t\treturn root;\n\t}\n \n\tpublic TreeNode bstFromPreorder(int[] preorder) { \... | 27 | 2 | [] | 3 |
construct-binary-search-tree-from-preorder-traversal | Morris' algorithm (O(n) time / O(n) space) | morris-algorithm-on-time-on-space-by-_ma-0l8l | Hi there!\nI have just solved that problem and started searching for the other solutions that might interest me. However, all users are solving it with the help | _maximus_ | NORMAL | 2020-04-20T13:58:42.421589+00:00 | 2020-05-25T10:49:57.425036+00:00 | 2,720 | false | Hi there!\nI have just solved that problem and started searching for the other solutions that might interest me. However, all users are solving it with the help of recursion or stack, and please do not get me wrong those are great solutions, I suggest you try Morris\' algorithm.\n\n**Explanation:**\nThat algorithm diff... | 27 | 1 | ['Python3'] | 8 |
construct-binary-search-tree-from-preorder-traversal | C++ iterative O(n) solution using decreasing stack | c-iterative-on-solution-using-decreasing-lc1i | iterate the input array\nfor each number x, we need to find the first number p less than x from the beginning of the array\nx should be the right child of p\nth | lc19890306 | NORMAL | 2019-03-10T15:02:01.616218+00:00 | 2019-09-02T04:40:30.465780+00:00 | 3,024 | false | iterate the input array\nfor each number x, we need to find the first number p less than x from the beginning of the array\nx should be the right child of p\nthus, we need to maintain a decreasing stack\nfor each number x in the array, we try to find the first number p less than x in the stack and make x the right chil... | 25 | 1 | [] | 5 |
construct-binary-search-tree-from-preorder-traversal | [C++] Straightforward iterative approach (100%/100%) | c-straightforward-iterative-approach-100-zjmw | Here is my iterative approach to solving the problem. I hope this helps!\n\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\ | jessephams | NORMAL | 2019-04-11T16:58:35.790606+00:00 | 2019-04-11T16:58:35.790700+00:00 | 3,145 | false | Here is my iterative approach to solving the problem. I hope this helps!\n\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n Tre... | 22 | 2 | ['C', 'Iterator', 'C++'] | 4 |
construct-binary-search-tree-from-preorder-traversal | C++ || O(n) || New Approach || No need to figure out Upper Bound or Max_Val | c-on-new-approach-no-need-to-figure-out-sxcpu | OBSERVATION :\nFor any Tree, the Preorder traversal is of this type : \n| root | left subtree | right subtree |\n\nSo, if we are able to identify the index of e | hritesh_bhardwaj | NORMAL | 2022-01-24T09:13:01.941753+00:00 | 2022-01-24T09:19:44.851896+00:00 | 2,011 | false | **OBSERVATION :**\nFor any Tree, the Preorder traversal is of this type : \n| root | left subtree | right subtree |\n\nSo, if we are able to identify the index of ending of left subtree in the given vector preorder.\nThen, we can easily defferentiate the left subtree part with the right subtree part and build the rest ... | 19 | 0 | ['Tree', 'Depth-First Search', 'Recursion', 'C', 'Binary Tree', 'C++'] | 4 |
construct-binary-search-tree-from-preorder-traversal | python beat 100% | python-beat-100-by-jason003-qr05 | python\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n if not preorder: return None\n root = TreeNode(preorder | jason003 | NORMAL | 2019-03-23T12:23:26.430229+00:00 | 2019-03-23T12:23:26.430284+00:00 | 2,200 | false | ```python\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n if not preorder: return None\n root = TreeNode(preorder[0])\n i = 1\n while i < len(preorder):\n if preorder[i] < preorder[0]: i += 1\n else: break\n root.left = self.bs... | 19 | 2 | [] | 5 |
construct-binary-search-tree-from-preorder-traversal | Video solution | Intuition and Approach | Detailed explanation | 🔥🔥🔥 | video-solution-intuition-and-approach-de-1sja | Video \nhey everyone, i have created a video solution for this problem and tried to explain this very intuitively, this video is part of trees playlist.\n\nVide | _code_concepts_ | NORMAL | 2024-08-14T11:38:43.380618+00:00 | 2024-08-14T11:38:43.380650+00:00 | 1,711 | false | # Video \nhey everyone, i have created a video solution for this problem and tried to explain this very intuitively, this video is part of trees playlist.\n\nVideo link: https://youtu.be/jOLLblvmQA0\nPlaylist link: https://www.youtube.com/playlist?list=PLICVjZ3X1Aca0TjUTcsD7mobU001CE7GL\n# Code\n```\n/**\n * Definition... | 18 | 0 | ['C++'] | 1 |
construct-binary-search-tree-from-preorder-traversal | A Morris Stype method + summary of 4 methods | a-morris-stype-method-summary-of-4-metho-clue | 1 morris algorithm\n\n\nThis is in essence similar to the stack solution below, but use the dictionary to eliminate stack. The idea just came to me and I think | newruanxy | NORMAL | 2020-05-24T08:21:15.261598+00:00 | 2020-05-24T08:22:25.936544+00:00 | 1,757 | false | # 1 morris algorithm\n\n\nThis is in essence similar to the stack solution below, but use the dictionary to eliminate stack. The idea just came to me and I think the inspiration came from morris algorithm.\n\n```python\ndef bstFromPreorder(self, ... | 18 | 0 | ['Python'] | 2 |
construct-binary-search-tree-from-preorder-traversal | JavaScript solution inspired by lee215 with explanation, O(n) time, O(h) space. | javascript-solution-inspired-by-lee215-w-p7f5 | Solution Explanation\n\nThis solution is inspired by this post written by lee215.\n\nThe main takeaway points in this solutions are:\n\n1. Every node has an upp | ecgan | NORMAL | 2020-04-20T17:00:44.913446+00:00 | 2020-04-20T17:00:44.913499+00:00 | 1,684 | false | ## Solution Explanation\n\nThis solution is inspired by [this post](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/discuss/252232/JavaC%2B%2BPython-O(N)-Solution) written by [lee215](https://leetcode.com/lee215/).\n\nThe main takeaway points in this solutions are:\n\n1. Every node ha... | 18 | 0 | ['JavaScript'] | 2 |
construct-binary-search-tree-from-preorder-traversal | c++ Easy straight forward O(N) recursive code | c-easy-straight-forward-on-recursive-cod-vxwa | \n TreeNode* bstFromPreorder1(vector<int>& preorder) {\n int pos =0;\n return bst(preorder, pos, INT_MAX, INT_MIN); \n }\n \n TreeNode | elbanna25 | NORMAL | 2020-05-17T16:21:12.580471+00:00 | 2020-05-17T16:23:44.958439+00:00 | 1,758 | false | ```\n TreeNode* bstFromPreorder1(vector<int>& preorder) {\n int pos =0;\n return bst(preorder, pos, INT_MAX, INT_MIN); \n }\n \n TreeNode*bst(vector<int>&preorder, int &pos, int max, int min){\n if (pos>=preorder.size()) return NULL;\n int val = preorder[pos];\n if (val > ... | 13 | 1 | ['C', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Java: Simple solution to place the nodes to tree one-by-one (100%/100%) | java-simple-solution-to-place-the-nodes-eqs3m | \nclass Solution {\n \n public void addToTree(TreeNode n, int val) {\n if (val < n.val) {\n if (n.left == null) {\n n.lef | mlalma | NORMAL | 2019-11-19T12:00:05.716680+00:00 | 2019-11-24T13:33:23.990753+00:00 | 1,249 | false | ```\nclass Solution {\n \n public void addToTree(TreeNode n, int val) {\n if (val < n.val) {\n if (n.left == null) {\n n.left = new TreeNode(val); \n } else {\n addToTree(n.left, val);\n }\n } else {\n if (n.rig... | 13 | 0 | ['Java'] | 1 |
construct-binary-search-tree-from-preorder-traversal | Python 1-liner | python-1-liner-by-ekovalyov-tds5 | \nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n if preorder:\n return TreeNode(preorder[0],\n | ekovalyov | NORMAL | 2020-05-24T12:00:31.765757+00:00 | 2020-05-24T12:00:31.765791+00:00 | 925 | false | ```\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n if preorder:\n return TreeNode(preorder[0],\n self.bstFromPreorder(tuple(takewhile(lambda x:x<preorder[0], preorder[1:]))),\n self.bstFromPreorder(tuple(dropwhi... | 12 | 0 | [] | 4 |
construct-binary-search-tree-from-preorder-traversal | [C++] 0ms Concise Solution | Beats 100% Time | c-0ms-concise-solution-beats-100-time-by-dqzu | \n TreeNode* bstFromPreorder(vector<int>& preorder) {\n if(preorder.size() == 0) return NULL;\n TreeNode* root = NULL;\n for(int i : pre | sherlasd | NORMAL | 2020-04-21T06:58:56.332713+00:00 | 2020-04-21T07:01:39.735931+00:00 | 889 | false | ```\n TreeNode* bstFromPreorder(vector<int>& preorder) {\n if(preorder.size() == 0) return NULL;\n TreeNode* root = NULL;\n for(int i : preorder)\n root = InsertBst(root, i);\n return root;\n }\n \n TreeNode* InsertBst(TreeNode* root, int i){\n if(!root) return ... | 12 | 0 | ['C', 'C++'] | 3 |
construct-binary-search-tree-from-preorder-traversal | JAVA 100% FASTER SOLUTION🤯🤯|| RECURSION🙂 | java-100-faster-solution-recursion-by-ab-rjx7 | Intuition\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 | abhiyadav05 | NORMAL | 2023-04-17T14:04:43.600428+00:00 | 2023-04-17T14:04:43.600460+00:00 | 1,706 | false | # Intuition\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)$$ -->O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$... | 11 | 0 | ['Tree', 'Binary Search Tree', 'Binary Tree', 'Java'] | 0 |
construct-binary-search-tree-from-preorder-traversal | javascript, simple recursive | javascript-simple-recursive-by-carti-qjwn | \n/**\n * @param {number[]} preorder\n * @return {TreeNode}\n */\nconst bstFromPreorder = (preorder) => {\n // base cases\n if (preorder.length === 0) return | carti | NORMAL | 2020-04-12T00:53:44.118488+00:00 | 2020-04-22T01:01:40.981315+00:00 | 1,072 | false | ```\n/**\n * @param {number[]} preorder\n * @return {TreeNode}\n */\nconst bstFromPreorder = (preorder) => {\n // base cases\n if (preorder.length === 0) return null;\n if (preorder.length === 1) return new TreeNode(preorder[0]);\n\n // result tree node\n var res = new TreeNode(preorder[0]);\n\n // iterate from 1... | 11 | 0 | ['Recursion', 'JavaScript'] | 3 |
construct-binary-search-tree-from-preorder-traversal | 💡JavaScript Solution | javascript-solution-by-aminick-5mlf | The idea\n1. Utilize the BST\u2018s peculiarity, which is nodes in the left subtree is smaller than the root, and nodes in the right subtree is bigger than the | aminick | NORMAL | 2020-01-31T11:39:53.808923+00:00 | 2020-01-31T11:39:53.809019+00:00 | 1,077 | false | ### The idea\n1. Utilize the BST\u2018s peculiarity, which is nodes in the left subtree is smaller than the root, and nodes in the right subtree is bigger than the root. Use this as a constraint, we can identify subtrees within the preorder array.\n```\n/**\n * @param {number[]} preorder\n * @return {TreeNode}\n */\nva... | 11 | 0 | ['JavaScript'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Simple java solution | simple-java-solution-by-traychev-o3g8 | \nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n TreeNode root = new TreeNode(preorder[0]);\n for (int i = 1; i < preor | traychev | NORMAL | 2019-03-31T12:27:50.552342+00:00 | 2019-03-31T12:27:50.552371+00:00 | 2,439 | false | ```\nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n TreeNode root = new TreeNode(preorder[0]);\n for (int i = 1; i < preorder.length; i++) {\n insertIntoBSThelper(root, preorder[i]);\n }\n return root;\n }\n \n public static void insertIntoBSThe... | 11 | 1 | [] | 3 |
construct-binary-search-tree-from-preorder-traversal | C++ || pre-order style insertion || iterative || beats 100% / 90.22% | c-pre-order-style-insertion-iterative-be-7qix | This is an amazing solution involving stacks. \n\nAlgorithm:\n\n1) 1st create the 1st element as root and assign an iterator p to it.\n2) Traverse the preorder | sherwin31 | NORMAL | 2021-05-03T20:24:20.901616+00:00 | 2021-05-03T20:29:34.915237+00:00 | 681 | false | This is an amazing solution involving stacks. \n\nAlgorithm:\n\n1) 1st create the 1st element as root and assign an iterator p to it.\n2) Traverse the preorder array from index 1 to preorder.size() - 1 (as you\'ve already created 1 node).\n3) Now if the preorder[i] is smaller than p->val, it means this node will lie to... | 10 | 0 | ['Stack', 'C', 'Iterator'] | 2 |
construct-binary-search-tree-from-preorder-traversal | 🌺22. Recursive [DFS] || Without Stack || No Extra Space || Short & Sweet Code || C++ Code Reference | 22-recursive-dfs-without-stack-no-extra-4u2e0 | Intuition\n1. All Elements in Left Subtree Should Be Smaller Than ROOT\n2. All Elements in Right Subtree Should Be Greater Than ROOT\n\n\n# Approach\n- Traverse | Amanzm00 | NORMAL | 2024-07-09T03:29:20.308456+00:00 | 2024-07-09T03:32:26.782036+00:00 | 673 | false | # Intuition\n1. All Elements in Left Subtree Should Be Smaller Than ROOT\n2. All Elements in Right Subtree Should Be Greater Than ROOT\n\n\n# Approach\n- Traverse Pre Order Wise \n1. We keep Track Of Bound , Which Is Upper Bound \n2. Elem. Should be Less Than Upper Bound\n\n# Code\n```cpp [There You GO !!]\n\nclass Sol... | 9 | 0 | ['Array', 'Stack', 'Tree', 'Binary Search Tree', 'Recursion', 'Monotonic Stack', 'Binary Tree', 'C++'] | 1 |
construct-binary-search-tree-from-preorder-traversal | ✅100% Accepted || Worst to Best approaches with explanation || Easy to understand. | 100-accepted-worst-to-best-approaches-wi-fx81 | Read the below approaches to understand the logic.\n\nPlease upvote if you like it\n\nApproach 1 : Create tree from given elements approach(Not optimised)\n Fir | pranjal9424 | NORMAL | 2022-10-02T09:32:58.848569+00:00 | 2022-10-02T09:32:58.848628+00:00 | 1,294 | false | **Read the below approaches to understand the logic.**\n\n***Please upvote if you like it***\n\n**Approach 1 : Create tree from given elements approach(Not optimised)**\n* First approach could be just create build tree fucntion pass preorder elements one by one and set new node at correct possition by just passing tree... | 9 | 0 | ['Binary Search Tree', 'Recursion', 'C', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | O(n) recursive solution | on-recursive-solution-by-pratham7711-57fx | \nclass Solution {\npublic:\n \n TreeNode* bstFromPreorder(vector<int>& preorder) {\n return build(preorder,0,preorder.size() - 1);\n }\n | pratham7711 | NORMAL | 2021-10-13T19:46:24.390597+00:00 | 2021-10-13T19:46:24.390626+00:00 | 182 | false | ```\nclass Solution {\npublic:\n \n TreeNode* bstFromPreorder(vector<int>& preorder) {\n return build(preorder,0,preorder.size() - 1);\n }\n \n TreeNode* build(vector<int> preorder , int start , int end)\n {\n if(start > end)\n return NULL;\n \n \n ... | 8 | 0 | ['Binary Tree'] | 0 |
construct-binary-search-tree-from-preorder-traversal | ✅✅ 2 METHODS || BEATS 100% || C++ CODE ✅✅ | 2-methods-beats-100-c-code-by-neelam_bin-k7tk | METHOD 1:IntuitionWe can build the binary search tree (BST) by following the properties of BSTs and recursively constructing subtrees based on bounds derived fr | neelam_bind | NORMAL | 2025-01-06T19:45:05.908274+00:00 | 2025-01-06T19:45:05.908274+00:00 | 1,013 | false | # METHOD 1:
# Intuition
We can build the binary search tree (BST) by following the properties of BSTs and recursively constructing subtrees based on bounds derived from the preorder traversal.
# Approach
1. Use preorder traversal to process each node.
2. Recursively build the left and right subtrees by updating bounds... | 7 | 0 | ['Binary Search Tree', 'Binary Tree', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | ✅💯🔥Simple Code🚀📌|| 🔥✔️Easy to understand🎯 || 🎓🧠Beats 100%🔥|| Beginner friendly💀💯 | simple-code-easy-to-understand-beats-100-hf8f | Solution tuntun mosi ki photo ke baad hai. Scroll Down\n\n\n# Code\njava []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int | atishayj4in | NORMAL | 2024-08-20T21:01:21.206117+00:00 | 2024-08-20T21:01:21.206139+00:00 | 858 | false | Solution tuntun mosi ki photo ke baad hai. Scroll Down\n\n\n# Code\n```java []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * ... | 7 | 0 | ['Array', 'Stack', 'Tree', 'Binary Search Tree', 'Monotonic Stack', 'Binary Tree', 'Python', 'C++', 'Java', 'JavaScript'] | 1 |
construct-binary-search-tree-from-preorder-traversal | 💯🔥✅Python Simple solution | explained properly | python-simple-solution-explained-properl-x1md | Solution Explanation for Constructing a BST from Preorder Traversal \uD83C\uDF33\n\nIn this solution, we aim to construct a binary search tree (BST) from a give | sagarsaini_ | NORMAL | 2024-07-26T05:11:03.203351+00:00 | 2024-07-26T05:11:03.203376+00:00 | 457 | false | ## Solution Explanation for Constructing a BST from Preorder Traversal \uD83C\uDF33\n\nIn this solution, we aim to construct a binary search tree (BST) from a given preorder traversal list. The key insight is that in a preorder traversal, the first element is always the root of the tree (or subtree), and the subsequent... | 7 | 0 | ['Array', 'Binary Search Tree', 'Binary Tree', 'Python3'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Brute - Better - Optimal [For Interview] 📄✅ | brute-better-optimal-for-interview-by-de-y7w1 | \nclass Solution {\npublic:\n \n// BRUTE [T(O(N*N))] [S(O(1))]\n \n TreeNode* insertNode(TreeNode* root,int key)\n {\n if(!root) return new T | debugagrawal | NORMAL | 2022-01-14T07:37:20.872398+00:00 | 2022-01-21T04:59:40.403194+00:00 | 407 | false | ```\nclass Solution {\npublic:\n \n// BRUTE [T(O(N*N))] [S(O(1))]\n \n TreeNode* insertNode(TreeNode* root,int key)\n {\n if(!root) return new TreeNode(key);\n if(key>root->val) root->right=insertNode(root->right,key);\n else root->left=insertNode(root->left,key);\n return r... | 7 | 0 | ['Tree', 'Recursion', 'C'] | 2 |
construct-binary-search-tree-from-preorder-traversal | Java monotonic stack solution (Converting Medium to Easy problem) | java-monotonic-stack-solution-converting-z6p0 | Below solution uses monotonic stack technique.\n\n Inserting Left node: If the current node is smaller than the node in top of the stack, then it is guarenteed | anirudhbm11 | NORMAL | 2021-11-19T06:12:04.458097+00:00 | 2021-11-19T06:12:04.458131+00:00 | 531 | false | Below solution uses monotonic stack technique.\n\n* *Inserting Left node*: If the current node is smaller than the node in top of the stack, then it is guarenteed that node lies in left half of BST.\n* *Inserting Right node*: It gets little tricky in this situtation but if you are aware of the monotonic stack concept... | 7 | 0 | ['Monotonic Stack', 'Java'] | 2 |
construct-binary-search-tree-from-preorder-traversal | Clean Python recursion + binary search solution w/ explanation beats 87% | clean-python-recursion-binary-search-sol-d7en | Recursion solution\n\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n if not preorder:\n return None\n | chiou | NORMAL | 2020-05-24T07:53:38.790021+00:00 | 2020-05-24T07:58:06.941234+00:00 | 704 | false | Recursion solution\n```\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n if not preorder:\n return None\n root = TreeNode(preorder[0])\n if len(preorder) == 1:\n return root\n\n\t\t# binary search to find the start of right subtree\n l... | 7 | 1 | ['Python'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Simple Java solution | simple-java-solution-by-susmitasingh09-jkzz | \nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n int n = preorder.length;\n if (n == 0) return null;\n TreeNode | susmitasingh09 | NORMAL | 2020-04-20T11:39:01.034084+00:00 | 2020-04-20T11:39:01.034133+00:00 | 718 | false | ```\nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n int n = preorder.length;\n if (n == 0) return null;\n TreeNode res = new TreeNode(preorder[0]);\n \n for (int i = 1; i < n; i++) {\n res = insert(res, preorder[i]);\n }\n return res... | 7 | 0 | [] | 2 |
construct-binary-search-tree-from-preorder-traversal | Python easy to understand 96% faster (iterative) | python-easy-to-understand-96-faster-iter-5rhg | \n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.righ | pujanm | NORMAL | 2020-04-20T07:57:46.668039+00:00 | 2020-04-20T18:07:38.177821+00:00 | 2,393 | false | ```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n if len(preorder) == 0:\n return\n \n ... | 7 | 0 | ['Binary Search', 'Tree', 'Python', 'Python3'] | 3 |
construct-binary-search-tree-from-preorder-traversal | 6 lines Javascript solution | 6-lines-javascript-solution-by-dnshi-8fiy | javascript\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/ | dnshi | NORMAL | 2019-06-30T00:06:17.617133+00:00 | 2019-06-30T00:06:17.617216+00:00 | 711 | false | ```javascript\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/**\n * @param {number[]} preorder\n * @return {TreeNode}\n */\nvar bstFromPreorder = function(preorder) {\n if (!preorder.length) return null\n \n c... | 7 | 1 | ['JavaScript'] | 4 |
construct-binary-search-tree-from-preorder-traversal | [C++] Easy to understand Solution with detail approach | c-easy-to-understand-solution-with-detai-acxv | Intuition\nThe problem requires constructing a binary search tree (BST) from a given preorder traversal of its nodes. The code uses a recursive approach to buil | Aditya00 | NORMAL | 2023-05-29T10:06:47.695501+00:00 | 2023-05-29T10:08:30.452287+00:00 | 2,165 | false | # Intuition\nThe problem requires constructing a binary search tree (BST) from a given preorder traversal of its nodes. The code uses a recursive approach to build the BST by dividing the problem into smaller subproblems.\n\n# Approach\n1. The bstFromPreorder function takes the given preorder traversal as input.\n2. It... | 6 | 0 | ['Graph', 'Binary Search Tree', 'Recursion', 'Binary Tree', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ | 100% faster | O(n) recursive solution | c-100-faster-on-recursive-solution-by-am-ecm5 | \n TreeNode* build(vector<int> pre, int s, int e){\n if(s>e) return NULL;\n TreeNode* root = new TreeNode(pre[s]);\n int idx;\n for(int i=s+1; i<=e | ama29n | NORMAL | 2021-10-13T19:12:30.740224+00:00 | 2021-10-13T19:12:30.740255+00:00 | 135 | false | ```\n TreeNode* build(vector<int> pre, int s, int e){\n if(s>e) return NULL;\n TreeNode* root = new TreeNode(pre[s]);\n int idx;\n for(int i=s+1; i<=e; i++){\n if(pre[i]>pre[s]){\n idx = i;\n break; }\n\t}\n root->left = build(pre, s+1, idx-1);\n root->right = build(pre, idx, e);\n... | 6 | 2 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | Java || Easy Approach with Explanation || Recursive || Postorder | java-easy-approach-with-explanation-recu-5uhv | \nclass Solution \n{\n public TreeNode bstFromPreorder(int[] preorder) \n {\n if(root == null)//base case when we are provide with the null graph \ | swapnilGhosh | NORMAL | 2021-07-27T06:53:48.437980+00:00 | 2021-07-27T06:53:48.438006+00:00 | 636 | false | ```\nclass Solution \n{\n public TreeNode bstFromPreorder(int[] preorder) \n {\n if(root == null)//base case when we are provide with the null graph \n return null;\n \n return constructBST(preorder, 0, preorder.length-1);//it returns the address of the new root of the BST, with al... | 6 | 0 | ['Depth-First Search', 'Recursion', 'Java'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Easy Recursive solution [C++] | easy-recursive-solution-c-by-harshghlt25-om2w | \nclass Solution {\npublic:\n TreeNode* insert(TreeNode* root, int value)\n {\n if(root==NULL)\n return new TreeNode(value);\n if | harshghlt25 | NORMAL | 2021-05-11T18:36:09.226027+00:00 | 2021-05-11T18:36:09.226071+00:00 | 669 | false | ```\nclass Solution {\npublic:\n TreeNode* insert(TreeNode* root, int value)\n {\n if(root==NULL)\n return new TreeNode(value);\n if(value<root->val)\n root->left=insert(root->left,value);\n if(value>root->val)\n root->right=insert(root->right,value);\n ... | 6 | 1 | ['Recursion', 'C', 'C++'] | 1 |
construct-binary-search-tree-from-preorder-traversal | Python - Concise Solution | python-concise-solution-by-nuclearoreo-zmmm | You pop the first value in the list and make it into node. Next you split the list of values to less than the node as left and larger than the node has right. C | nuclearoreo | NORMAL | 2020-04-20T19:15:47.545337+00:00 | 2020-04-21T13:15:59.685228+00:00 | 740 | false | You pop the first value in the list and make it into node. Next you split the list of values to less than the node as left and larger than the node has right. Continue on untill there\'s no numbers to use. **Note**, you have to set the `index` variable to the length of the list for the case that there\'s no number larg... | 6 | 0 | ['Depth-First Search', 'Python'] | 1 |
construct-binary-search-tree-from-preorder-traversal | JS easy to read preorder traversal (92%runtime/80%space) | js-easy-to-read-preorder-traversal-92run-ydsm | \nvar bstFromPreorder = function(preorder) {\n let root = new TreeNode(preorder[0])\n for (let i = 1; i < preorder.length; i++) {\n appendToTreeNod | GSWE | NORMAL | 2020-04-20T16:02:43.127520+00:00 | 2020-04-20T16:02:43.127602+00:00 | 268 | false | ```\nvar bstFromPreorder = function(preorder) {\n let root = new TreeNode(preorder[0])\n for (let i = 1; i < preorder.length; i++) {\n appendToTreeNode(root, preorder[i])\n }\n return root\n};\n\nfunction appendToTreeNode(root, val) {\n if (val <= root.val) {\n if (root.left) appendToTreeNo... | 6 | 0 | [] | 1 |
construct-binary-search-tree-from-preorder-traversal | simple recursive insert in bst in CPP | simple-recursive-insert-in-bst-in-cpp-by-vcnw | \nTreeNode* Insert(TreeNode* root,int data){\n if(root == NULL){\n root = new TreeNode(data);\n }\n else if(root->val > data){\n | gowtham_1 | NORMAL | 2020-04-20T09:17:01.546968+00:00 | 2020-04-20T09:17:01.547002+00:00 | 391 | false | ```\nTreeNode* Insert(TreeNode* root,int data){\n if(root == NULL){\n root = new TreeNode(data);\n }\n else if(root->val > data){\n root->left = Insert(root->left,data);\n }\n else{\n root->right = Insert(root->right,data);\n }\n return r... | 6 | 0 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | Python O(N) Stack based Solution beat 96% | python-on-stack-based-solution-beat-96-b-uujt | The basic idea is using a Stack to keep values, everytime you meet a new value smaller than the end of the stack, you know this new value should be left node of | tanzhenyu2015 | NORMAL | 2019-10-18T03:28:10.017197+00:00 | 2019-10-18T03:29:41.558311+00:00 | 351 | false | The basic idea is using a Stack to keep values, everytime you meet a new value smaller than the end of the stack, you know this new value should be left node of it; Otherwise keep popping the stack until you find the last node whose value is smaller than the new value, you know this new value should be right node of it... | 6 | 0 | [] | 2 |
construct-binary-search-tree-from-preorder-traversal | 3 BEST C++ solutions || Using INT_MAX, recursive and iterative approach || Beast 100% !! | 3-best-c-solutions-using-int_max-recursi-ekjw | Code\n\n// Solution 1 - Using max bound\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& pre) {\n int i = 0;\n return build( | prathams29 | NORMAL | 2023-08-30T02:18:50.547037+00:00 | 2023-08-30T02:18:50.547054+00:00 | 548 | false | # Code\n```\n// Solution 1 - Using max bound\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& pre) {\n int i = 0;\n return build(pre, i, INT_MAX);\n }\n\n TreeNode* build(vector<int>& pre, int& i, int bound) {\n if (i == pre.size() || pre[i] > bound) \n return... | 5 | 0 | ['Array', 'Stack', 'Tree', 'Binary Search Tree', 'Monotonic Stack', 'Binary Tree', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Superb logic | superb-logic-by-ganjinaveen-27of | \nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:\n inorder=sorted(preorder)\n def build(preorder,inord | GANJINAVEEN | NORMAL | 2023-07-11T08:53:40.666442+00:00 | 2023-07-11T08:53:40.666462+00:00 | 675 | false | ```\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:\n inorder=sorted(preorder)\n def build(preorder,inorder):\n if not preorder or not inorder:\n return \n root=TreeNode(preorder[0])\n mid=inorder.index(preorder[0]... | 5 | 0 | ['Python', 'Python3'] | 1 |
construct-binary-search-tree-from-preorder-traversal | ✔ C++ | All Tree Construction based Questions at a single place | 2 Approaches of each | c-all-tree-construction-based-questions-htid1 | \n### Table of Content :\n\n Construct BST from Preorder Traversal\n\t Recursive Approach\n\t Iterative Approach using Stack\n Construct BST from Postorder Trav | HustlerNitin | NORMAL | 2022-08-30T18:05:24.328527+00:00 | 2022-08-30T18:43:58.612058+00:00 | 451 | false | ***\n### Table of Content :\n***\n* Construct **BST** from **Preorder** Traversal\n\t* Recursive Approach\n\t* Iterative Approach using Stack\n* Construct **BST** from **Postorder** Traversal\n\t* Iterative Approach using Stack\n\t* Recursive ( using sorted Inorder property of BST )\n* Construct **Binary Tree** using *... | 5 | 0 | ['Stack', 'Binary Search Tree', 'C', 'Binary Tree', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | [Python3] O(n) Time Solution 2 Approaches (using Stack and Sorted Preorder) | python3-on-time-solution-2-approaches-us-cxte | Approach 1 - BST from Preorder and Inorder(Sorted Preorder) arrays\n\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, l | SamirPaulb | NORMAL | 2022-06-02T14:30:35.089626+00:00 | 2022-06-02T14:31:14.682379+00:00 | 812 | false | Approach 1 - **BST from Preorder and Inorder(Sorted Preorder) arrays**\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 bstFromPreorder(self... | 5 | 0 | ['Array', 'Stack', 'Binary Search Tree', 'Sorting', 'Iterator', 'Python', 'Python3'] | 1 |
construct-binary-search-tree-from-preorder-traversal | C++ || recursive solution || short and clean!!! | c-recursive-solution-short-and-clean-by-xpka9 | \nclass Solution {\npublic:\n TreeNode* builtTree(vector<int>& preorder, int &prevIndex, int &boundaryVal)\n {\n if(prevIndex>=preorder.size() || p | priya_07 | NORMAL | 2022-02-02T19:00:57.404835+00:00 | 2022-02-02T19:00:57.404863+00:00 | 402 | false | ```\nclass Solution {\npublic:\n TreeNode* builtTree(vector<int>& preorder, int &prevIndex, int &boundaryVal)\n {\n if(prevIndex>=preorder.size() || preorder[prevIndex]>=boundaryVal)\n return NULL;\n TreeNode* root=new TreeNode(preorder[prevIndex]);\n prevIndex++;\n root->le... | 5 | 0 | ['Binary Search Tree', 'Recursion', 'C', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | 0 ms, faster than 100.00% ; 13 MB, less than 90.67% [C++] | 0-ms-faster-than-10000-13-mb-less-than-9-orcn | ```\n/ first element is always the root. \nIterate over the rest array elements & \nperform bst insertion \ntaking first element as root every time /\n\nclass S | _ak_47 | NORMAL | 2020-12-05T21:11:25.625842+00:00 | 2020-12-05T21:11:25.625874+00:00 | 334 | false | ```\n/* first element is always the root. \nIterate over the rest array elements & \nperform bst insertion \ntaking first element as root every time */\n\nclass Solution {\nprivate:\n void insert(TreeNode* root,int val){\n TreeNode* ptr=root,*prev=NULL;\n while(ptr){\n if(val<ptr->val){\n ... | 5 | 1 | ['Binary Search Tree', 'C'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Simple C++ solution - Easy to understand | simple-c-solution-easy-to-understand-by-f0dcx | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0) | sanjay_prabhu | NORMAL | 2020-05-24T14:21:54.116838+00:00 | 2020-05-24T14:21:54.116873+00:00 | 569 | false | ```\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... | 5 | 0 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ | Recursive 100% | c-recursive-100-by-ruddrd09-l1lk | ```\nclass Solution {\npublic:\n TreeNode tree(vector& preorder, int i, int j) {\n if(i > j) {\n return NULL;\n }\n TreeNode | ruddrd09 | NORMAL | 2020-05-24T07:24:39.064554+00:00 | 2020-05-24T07:24:39.064611+00:00 | 553 | false | ```\nclass Solution {\npublic:\n TreeNode* tree(vector<int>& preorder, int i, int j) {\n if(i > j) {\n return NULL;\n }\n TreeNode* root = new TreeNode(preorder[i]);\n if(i == j) {\n return root;\n }\n int k = i+1;\n while(k <= j) {\n ... | 5 | 1 | ['C'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Python3. Simple, clean and easy to understand solution | python3-simple-clean-and-easy-to-underst-ecas | ```\nclass Solution:\n def bstFromPreorder(self, preorder):\n root = None\n \n for e in preorder:\n root = self.insert(root, e)\n | abstractart | NORMAL | 2020-04-20T18:40:50.129132+00:00 | 2020-04-25T16:55:05.147219+00:00 | 469 | false | ```\nclass Solution:\n def bstFromPreorder(self, preorder):\n root = None\n \n for e in preorder:\n root = self.insert(root, e)\n \n return root\n\n \n def insert(self, node, val):\n if node is None:\n return TreeNode(val) \n \n if val < node.val:\n ... | 5 | 0 | ['Python3'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Python beats 99% | python-beats-99-by-palash2594-ziiu | \n# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# s | palash2594 | NORMAL | 2019-06-20T03:29:12.786157+00:00 | 2019-06-20T03:29:12.786192+00:00 | 505 | false | ```\n# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution(object):\n \n def insert(self, root, val):\n if val <= root.val:\n if root.left != None:\n ... | 5 | 1 | [] | 2 |
construct-binary-search-tree-from-preorder-traversal | JS recursive | js-recursive-by-chux0519-0e7o | javascript\nfunction TreeNode(val) {\n this.val = val;\n this.left = this.right = null;\n}\nvar bstFromPreorder = function (preorder) {\n i | chux0519 | NORMAL | 2019-03-12T11:57:29.889717+00:00 | 2019-03-12T11:57:29.889758+00:00 | 338 | false | ```javascript\nfunction TreeNode(val) {\n this.val = val;\n this.left = this.right = null;\n}\nvar bstFromPreorder = function (preorder) {\n if (preorder.length === 0) return null\n if (preorder.length === 1) return new TreeNode(preorder[0])\n let root = new TreeNode(preorder[0])\n ... | 5 | 0 | [] | 1 |
construct-binary-search-tree-from-preorder-traversal | Java/C# Recursive solution beats 100% with clear explanation | javac-recursive-solution-beats-100-with-vxpfx | Thoughts: \nThe input array is the preorder traversal of a BST. \n1.preorder[0] is the root of BST\n2.The sub array from preorder[0] to the first element that i | skrmao | NORMAL | 2019-03-10T20:16:02.019867+00:00 | 2019-03-10T20:16:02.019910+00:00 | 649 | false | Thoughts: \nThe input array is the preorder traversal of a BST. \n1.preorder[0] is the root of BST\n2.The sub array from preorder[0] to the first element that is bigger than preorder[0] is the preorder traversal of the left subtree.\n The sub array from element that is bigger than preorder[0] to the end of the array i... | 5 | 1 | ['Recursion', 'Java'] | 2 |
construct-binary-search-tree-from-preorder-traversal | Optimal Approach| O(N) Space Complexity ⭐| Beats 100% | optimal-approach-on-space-complexity-bea-h0dy | Intuition\nThe problem involves constructing a binary search tree (BST) from its preorder traversal. Preorder traversal visits nodes in the order: root, left su | vaib8557 | NORMAL | 2024-06-15T00:01:58.880601+00:00 | 2024-06-15T00:01:58.880620+00:00 | 959 | false | # Intuition\nThe problem involves constructing a binary search tree (BST) from its preorder traversal. Preorder traversal visits nodes in the order: root, left subtree, right subtree. Given this order, we can reconstruct the BST by iteratively creating nodes and ensuring that each node respects the BST properties.\n\n#... | 4 | 0 | ['C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ | 4 different AC solutions : explained ⭐️ | c-4-different-ac-solutions-explained-by-ktgri | Code - 1 : using Next Greater Element Array (NGE)\n- We make an array NGE storing the index of the Next Greater Element of each element in the array, and then s | vanshdhawan60 | NORMAL | 2023-09-05T22:22:43.998471+00:00 | 2023-09-05T22:22:43.998498+00:00 | 172 | false | # Code - 1 : using Next Greater Element Array (NGE)\n- We make an array NGE storing the index of the Next Greater Element of each element in the array, and then simply construct our tree in preorder fashion using the indices we obtained through our NGE array.\n- This is because we know that all elements upto Next Great... | 4 | 0 | ['Array', 'Stack', 'Tree', 'Binary Search Tree', 'Monotonic Stack', 'Binary Tree', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Simple Recursive C++ O(nlogn) solution | simple-recursive-c-onlogn-solution-by-ia-349s | Complexity Analysis\n- Time complexity:\nO(nlogn) in average case, and O(n^2) in worst case\n\n- Space complexity:\nO(h) in average case, where h = height of BS | iamarunaksha | NORMAL | 2023-09-03T13:02:38.246639+00:00 | 2023-09-03T13:02:38.246661+00:00 | 245 | false | # Complexity Analysis\n- Time complexity:\nO(nlogn) in average case, and O(n$$^2$$) in worst case\n\n- Space complexity:\nO(h) in average case, where h = height of BST\nO(n) in worst case, if its a skew-tree\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNo... | 4 | 0 | ['Binary Search Tree', 'Recursion', 'Binary Tree', 'C++'] | 1 |
construct-binary-search-tree-from-preorder-traversal | Easy C++ Solution ✅ | 2 solution | Optimised and Optimal Solution | easy-c-solution-2-solution-optimised-and-esg9 | Intuition\nOptimised -> Inorder Traversal is required along with preOrder to create a unique tree. We can get that after sorting preOrder array.\n\nOptimal -> S | HariBhakt | NORMAL | 2023-06-06T17:09:38.126563+00:00 | 2023-06-06T17:09:38.126600+00:00 | 538 | false | # Intuition\nOptimised -> Inorder Traversal is required along with preOrder to create a unique tree. We can get that after sorting preOrder array.\n\nOptimal -> Since for any BST : [Left < Root < Right ]\nWe just to need keep adding nodes after comparing it with the root and its left and right subtree.\n\n# Approach\nO... | 4 | 0 | ['Recursion', 'Ordered Map', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Best O(N) Solution | best-on-solution-by-kumar21ayush03-6avs | Approach 1\nBrute-Force\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(1)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * str | kumar21ayush03 | NORMAL | 2023-03-09T08:50:26.286703+00:00 | 2023-03-09T08:50:26.286743+00:00 | 1,624 | false | # Approach 1\nBrute-Force\n\n# Complexity\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\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 * ... | 4 | 0 | ['C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Simple Approach Beats 100% in TC and 90% in SC. || Easy To Understand . | simple-approach-beats-100-in-tc-and-90-i-74i7 | Intuition\nFirst Elementr of PreOrder Traversal Array will be root always.\n\n# Approach\nSimply insert the elemnt of given array by comparing if it is greater | jigs_ | NORMAL | 2023-03-02T13:38:39.907206+00:00 | 2023-03-02T13:38:39.907242+00:00 | 334 | false | # Intuition\nFirst Elementr of PreOrder Traversal Array will be root always.\n\n# Approach\nSimply insert the elemnt of given array by comparing if it is greater or not ,\n\n# Complexity\n- Time complexity:\nO(N)\n\n\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * ... | 4 | 0 | ['C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | 1008. Construct Binary Search Tree from Preorder Traversal || most easy sol || java | 1008-construct-binary-search-tree-from-p-gpuc | // a very clever solution\n// one loop\n// fx call and check\n// see take an indivisual element and then call the fx for placing it \n// if you like the sol the | harjotse | NORMAL | 2022-11-03T18:47:06.478641+00:00 | 2022-11-03T18:47:06.478678+00:00 | 715 | false | // a very clever solution\n// one loop\n// fx call and check\n// see take an indivisual element and then call the fx for placing it \n// if you like the sol then pls upvote :)\n```\nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) \n {\n TreeNode root= new TreeNode(preorder[0]);\n for(int i: p... | 4 | 0 | ['Binary Search Tree', 'Binary Tree', 'Java'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Easy Java Solution | Using Recursion | easy-java-solution-using-recursion-by-pi-y8g1 | \nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n TreeNode root= new TreeNode( preorder[0] );\n \n for(int i: pre | PiyushParmar_28 | NORMAL | 2022-10-25T06:25:22.736107+00:00 | 2022-10-25T06:25:22.736147+00:00 | 984 | false | ```\nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n TreeNode root= new TreeNode( preorder[0] );\n \n for(int i: preorder){\n createTree(i, root);\n }\n \n return root;\n }\n \n public TreeNode createTree(int i, TreeNode root){\n ... | 4 | 0 | ['Recursion', 'Java'] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ || BST || 2 METHODS || EASY TO UNDERSATAND || STRIVER METHOD | c-bst-2-methods-easy-to-undersatand-stri-a34j | \n//--------------------------------1ST METHOD-----------------------------------------\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& p | _chintu_bhai | NORMAL | 2022-10-18T19:44:10.095757+00:00 | 2022-10-18T19:44:10.095792+00:00 | 649 | false | ```\n//--------------------------------1ST METHOD-----------------------------------------\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& preorder) {\n vector<int>inorder=preorder;\n sort(inorder.begin(),inorder.end());\n map<int,int>m;\n for(int i=0;i<inorder.size();... | 4 | 0 | ['Binary Search Tree', 'C'] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ || Two Different Approach || Optimised Solution with 0ms | c-two-different-approach-optimised-solut-r06j | \nclass Solution {\n \n TreeNode * Solve(vector<int>& preorder, int start ,int end)\n {\n if(start>end)\n return NULL;\n \n | Khwaja_Abdul_Samad | NORMAL | 2022-08-17T15:04:17.389040+00:00 | 2022-08-17T15:04:17.389072+00:00 | 555 | false | ```\nclass Solution {\n \n TreeNode * Solve(vector<int>& preorder, int start ,int end)\n {\n if(start>end)\n return NULL;\n \n TreeNode * root = new TreeNode(preorder[start]);\n \n int i = start+1;\n for(; i<=end ; i++)\n {\n if(preorder[st... | 4 | 0 | ['C', 'Iterator'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Python | JUST 6 Line Code | Own Solution | O(N) Time | python-just-6-line-code-own-solution-on-1jbk2 | \nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:\n if len(preorder)==0:\n return None\n roo | manjeet13 | NORMAL | 2022-05-27T18:24:47.761416+00:00 | 2022-05-27T20:20:12.577335+00:00 | 412 | false | ```\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:\n if len(preorder)==0:\n return None\n root=TreeNode(preorder[0])\n root.left=self.bstFromPreorder([i for i in preorder if i<root.val])\n root.right=self.bstFromPreorder([i for i in pre... | 4 | 0 | ['Binary Search Tree', 'Recursion', 'Python'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Discussed All Approach in Notes | C++ | JAVA | discussed-all-approach-in-notes-c-java-b-gs0q | Notes Link : https://github.com/rizonkumar/LeetCode-Notes/blob/main/1008.pdf\n\nJava Code\n\n\n\nclass Solution {\n public TreeNode bstFromPreorder(int[] pr | rizon__kumar | NORMAL | 2022-04-03T04:29:42.247396+00:00 | 2022-04-03T04:29:42.247440+00:00 | 73 | false | **Notes Link** : https://github.com/rizonkumar/LeetCode-Notes/blob/main/1008.pdf\n\nJava Code\n\n```\n\nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n return bstFromPreorder(preorder, Integer.MAX_VALUE, new int[] {0});\n }\n \n public TreeNode bstFromPreorder(int[] preorder, ... | 4 | 0 | ['Binary Search Tree'] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ | Easy + Efficient solution O(n) TC. | c-easy-efficient-solution-on-tc-by-jv02-je22 | \nclass Solution {\npublic:\n TreeNode* tt(vector<int>& p, int& i, int upperbound){\n if(i==p.size() || p[i]>upperbound) return NULL;\n TreeNod | jv02 | NORMAL | 2022-03-19T11:05:03.464510+00:00 | 2022-03-19T11:05:03.464537+00:00 | 159 | false | ```\nclass Solution {\npublic:\n TreeNode* tt(vector<int>& p, int& i, int upperbound){\n if(i==p.size() || p[i]>upperbound) return NULL;\n TreeNode* root = new TreeNode(p[i++]);\n root->left = tt(p,i,root->val);\n root->right = tt(p,i,upperbound);\n \n return root;\n ... | 4 | 0 | ['C'] | 0 |
construct-binary-search-tree-from-preorder-traversal | [C++] Recursive and Stack Iterative Solutions | c-recursive-and-stack-iterative-solution-cgpe | Recursive:\n\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& p) {\n int id = 0;\n return build(p, id, INT_MAX);\n }\n | ddliu6 | NORMAL | 2021-10-13T05:54:59.021445+00:00 | 2021-10-13T17:20:41.792783+00:00 | 441 | false | Recursive:\n```\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& p) {\n int id = 0;\n return build(p, id, INT_MAX);\n }\n \n TreeNode* build(vector<int>& p, int& id, int limit) {\n if(id == p.size() || p[id] > limit)\n return NULL;\n int val = p[id++... | 4 | 0 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ NO STACK, ONLY RECURSIVE CALLS BEATS 100% RUNTIME AND MEMORY | c-no-stack-only-recursive-calls-beats-10-1dzl | Make a function which will insert your elements from the preorder array into the binary search tree in the predefined form of the binary search tree.\n\n\nclass | messi1711 | NORMAL | 2021-10-13T02:13:12.041026+00:00 | 2021-10-13T02:13:12.041076+00:00 | 435 | false | Make a function which will insert your elements from the preorder array into the binary search tree in the predefined form of the binary search tree.\n\n```\nclass Solution {\npublic:\n TreeNode* bstFromPreorder(vector<int>& preorder) {\n TreeNode* root=new TreeNode();\n \n root->val=preorder[0]... | 4 | 0 | [] | 1 |
construct-binary-search-tree-from-preorder-traversal | Straightforward recursive solution via Python | straightforward-recursive-solution-via-p-yjon | \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# | tinfoilhat0 | NORMAL | 2021-10-13T00:14:52.744814+00:00 | 2021-10-13T00:14:52.744846+00:00 | 399 | false | ```\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 bstFromPreorder(self, preorder):\n """\n :type preorder: List[... | 4 | 0 | [] | 1 |
construct-binary-search-tree-from-preorder-traversal | 0ms | 100 beats | Self - Explanatory | JAVA | Recursion | 0ms-100-beats-self-explanatory-java-recu-f34d | self explanatory code,\njust make a node for each element and fit it into BST\n\n\n\n\npublic TreeNode bstFromPreorder(int[] preorder) \n {\n if(preor | rahulgilhotra | NORMAL | 2021-08-16T10:52:38.452169+00:00 | 2021-08-16T10:52:38.452200+00:00 | 479 | false | self explanatory code,\njust make a node for each element and fit it into BST\n\n\n\n```\npublic TreeNode bstFromPreorder(int[] preorder) \n {\n if(preorder.length==0) return null;\n \n TreeNode root = new TreeNode(preorder[0]);\n \n for(int i=1;i<preorder.length;i++)\n {\n ... | 4 | 0 | ['Recursion', 'Java'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Java Easy to understand | beats 100% on runtime | self explanatory | java-easy-to-understand-beats-100-on-run-v7id | ```\n/*\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {} | Backend_engineer123 | NORMAL | 2021-06-17T19:31:01.183396+00:00 | 2021-06-17T19:31:39.170872+00:00 | 292 | false | ```\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 = l... | 4 | 0 | ['Java'] | 1 |
construct-binary-search-tree-from-preorder-traversal | Python O(N) : Easy Recursive using BST property, min & max | python-on-easy-recursive-using-bst-prope-t8k1 | ```\nimport math\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n \n def construct(preorder,mn,mx):\n | knz_80 | NORMAL | 2021-04-22T05:40:06.883474+00:00 | 2021-04-22T05:40:06.883518+00:00 | 227 | false | ```\nimport math\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n \n def construct(preorder,mn,mx):\n if preorder:\n\t\t\t\t# Any node value violates bst property\n if preorder[0]<mn or preorder[0]>mx:\n return None\n ... | 4 | 0 | ['Python'] | 0 |
construct-binary-search-tree-from-preorder-traversal | simple c++ solution | recursive | simple-c-solution-recursive-by-xor09-bqoz | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0) | xor09 | NORMAL | 2021-02-17T17:28:39.112626+00:00 | 2021-02-17T17:28:39.112665+00:00 | 221 | false | ```\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... | 4 | 1 | ['Recursion', 'C'] | 1 |
construct-binary-search-tree-from-preorder-traversal | [C++] 5-line solution Time/Space: O(N) | c-5-line-solution-timespace-on-by-codeda-9zpo | \nclass Solution {\npublic:\n int i = 0;\n TreeNode* bstFromPreorder(vector<int>& pre, int upper=INT_MAX) { \n if(i >= pre.size() || pre[i] | codedayday | NORMAL | 2020-05-24T17:33:41.544974+00:00 | 2020-05-24T17:33:41.545014+00:00 | 290 | false | ```\nclass Solution {\npublic:\n int i = 0;\n TreeNode* bstFromPreorder(vector<int>& pre, int upper=INT_MAX) { \n if(i >= pre.size() || pre[i] > upper) return NULL;\n return new TreeNode(pre[i++], bstFromPreorder(pre, pre[i-1]), bstFromPreorder(pre, upper)); \n }\n};\n``` | 4 | 0 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | Python. No recursion. | python-no-recursion-by-techrabbit58-rt2w | \nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n node_stack = []\n node = root = TreeNode(preorder[0])\n | techrabbit58 | NORMAL | 2020-05-24T12:03:16.397005+00:00 | 2020-05-24T12:05:54.384511+00:00 | 580 | false | ```\nclass Solution:\n def bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n node_stack = []\n node = root = TreeNode(preorder[0])\n for n in preorder[1:]:\n if n <= node.val:\n node.left = TreeNode(n)\n node_stack.append(node)\n no... | 4 | 0 | ['Python', 'Python3'] | 2 |
construct-binary-search-tree-from-preorder-traversal | Recursive Java Solution | recursive-java-solution-by-ztztzt8888-116w | \nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n TreeNode root = new TreeNode(preorder[0]);\n for (int i = 1; i < preor | ztztzt8888 | NORMAL | 2020-04-24T10:22:08.693935+00:00 | 2020-04-24T10:22:08.693992+00:00 | 229 | false | ```\nclass Solution {\n public TreeNode bstFromPreorder(int[] preorder) {\n TreeNode root = new TreeNode(preorder[0]);\n for (int i = 1; i < preorder.length; i++) {\n helper(root, new TreeNode(preorder[i]));\n }\n return root;\n }\n\n private void helper(TreeNode root, Tr... | 4 | 0 | ['Binary Search Tree', 'Recursion'] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ BST Insert | c-bst-insert-by-eyan422-puar | C++\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) | eyan422 | NORMAL | 2020-04-20T09:05:19.328890+00:00 | 2020-04-20T09:05:19.328925+00:00 | 549 | false | ```C++\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic: \n TreeNode* BstInsert(TreeNode*& root, int val){\n if(!root){ \n... | 4 | 0 | [] | 1 |
construct-binary-search-tree-from-preorder-traversal | C# code easy to understand | c-code-easy-to-understand-by-fadi17-d62c | \n public TreeNode BstFromPreorder(int[] preorder) {\n if(preorder == null)\n return null;\n // root node will always be first node | fadi17 | NORMAL | 2019-05-26T10:08:56.956579+00:00 | 2020-04-21T07:15:17.512533+00:00 | 287 | false | ````\n public TreeNode BstFromPreorder(int[] preorder) {\n if(preorder == null)\n return null;\n // root node will always be first node in preorder traversal\n TreeNode root = new TreeNode(preorder[0]);\n \n // If we simply just insert all nodes in array \n // us... | 4 | 0 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | Java O(n) solution with range | java-on-solution-with-range-by-tohrah-isfd | \n\nclass Solution {\n int nodeIdx;\n public TreeNode bstFromPreorder(int[] preorder) {\n if(preorder == null) {\n return null;\n | tohrah | NORMAL | 2019-03-13T21:10:48.497013+00:00 | 2019-03-13T21:10:48.497099+00:00 | 1,058 | false | ```\n\nclass Solution {\n int nodeIdx;\n public TreeNode bstFromPreorder(int[] preorder) {\n if(preorder == null) {\n return null;\n }\n nodeIdx = 0;\n return bstHelper(preorder, Integer.MIN_VALUE , Integer.MAX_VALUE);\n }\n private TreeNode bstHelper(int[] preorder, i... | 4 | 0 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | Python O(N) short solution using min, max and deque | python-on-short-solution-using-min-max-a-0o6z | \ndef bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n def bst(pre, min, max):\n node = None\n if pre and min < pre[0] < m | Cubicon | NORMAL | 2019-03-10T04:00:50.867431+00:00 | 2019-03-10T04:00:50.867487+00:00 | 601 | false | ```\ndef bstFromPreorder(self, preorder: List[int]) -> TreeNode:\n def bst(pre, min, max):\n node = None\n if pre and min < pre[0] < max:\n node = TreeNode(pre[0])\n pre.popleft()\n node.left = bst(pre, min, node.val)\n node.right ... | 4 | 2 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | 100% Acceptance! ✅(0ms) Easy To Understand!✅ | 100-acceptance-0ms-easy-to-understand-by-ho0a | VELAN.MVelan.MComplexity
Time complexity: O(n2)✅
Space complexity:O(n)✅
Code | velan_m_velan | NORMAL | 2025-03-29T13:12:55.963631+00:00 | 2025-03-29T13:12:55.963631+00:00 | 286 | false | # VELAN.M
$$Velan.M$$
# Complexity
- Time complexity: $$O(n^2)$$✅
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:$$O(n)$$✅
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
TreeNode* bstFromPreorder(vector<int>& preorder) {
if(preorde... | 3 | 0 | ['Tree', 'Binary Search Tree', 'Binary Tree', 'C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Construct Binary Search Tree from Preorder Traversal | construct-binary-search-tree-from-preord-ezq1 | Complexity
Time complexity: O(N)
Space complexity: O(N)
Code | Ansh1707 | NORMAL | 2025-03-05T23:33:29.579989+00:00 | 2025-03-05T23:33:29.579989+00:00 | 208 | false |
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python []
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# ... | 3 | 0 | ['Stack', 'Binary Tree', 'Python'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Java solution || 0ms || 100%faster | java-solution-0ms-100faster-by-ramsingh2-63lk | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Ramsingh27 | NORMAL | 2025-01-10T07:04:11.149893+00:00 | 2025-01-10T07:04:11.149893+00:00 | 352 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 3 | 0 | ['Java'] | 0 |
construct-binary-search-tree-from-preorder-traversal | ✅✅BEST AND SIMPLE SOLUTION🤩🤩 | best-and-simple-solution-by-rishu_raj593-0j74 | Intuition\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 | Rishu_Raj5938 | NORMAL | 2024-09-12T14:16:27.599082+00:00 | 2024-09-12T14:16:27.599119+00:00 | 284 | false | # Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | just two recursion thats it | just-two-recursion-thats-it-by-sajal-we4k | Intuition\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 | Sajal_ | NORMAL | 2024-03-06T12:06:34.042462+00:00 | 2024-03-06T12:06:34.042498+00:00 | 861 | false | # Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['Python3'] | 0 |
construct-binary-search-tree-from-preorder-traversal | [C++] Solution using STACK . Unique approaching . Clean and Easy for understanding | c-solution-using-stack-unique-approachin-07ws | Intuition\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 | lshigami | NORMAL | 2024-01-13T06:44:46.637299+00:00 | 2024-01-13T06:44:46.637329+00:00 | 267 | false | # Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
construct-binary-search-tree-from-preorder-traversal | ||❤️☑️Best Approach using Range Approach☑️❤️|| Recurssive ||Java|| | best-approach-using-range-approach-recur-y1jm | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nThis program is solve i | kailashsahu07 | NORMAL | 2023-11-16T19:01:47.941270+00:00 | 2023-11-16T19:01:47.941300+00:00 | 302 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis program is solve in the approach using some range approach . first we have to find the range of all node using some recurssie approach then create node connect it... | 3 | 0 | ['Java'] | 0 |
construct-binary-search-tree-from-preorder-traversal | Python 3 EASY AND FAST/GOOD MEMORY✅✅✅ | python-3-easy-and-fastgood-memory-by-bla-onvu | \n\n# Approach\n Describe your approach to solving the problem. \n1. pick the first item as the root.\n2. get the numbers smaller and bigger.\n3. set bst\'s lef | Little_Tiub | NORMAL | 2023-09-05T21:38:39.163534+00:00 | 2023-09-05T21:38:39.163557+00:00 | 173 | false | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. pick the first item as the root.\n2. get the numbers smaller and bigger.\n3. set `bst`\'s left to (background noise: recursing on `smaller`)\n4. set `bst`\'s left to (background noise: recursing on `bigger`)\n\n# Complexity\n- Time complexity: ... | 3 | 0 | ['Python3'] | 0 |
construct-binary-search-tree-from-preorder-traversal | C++ Solution using 0(N) complexity using only preorder | c-solution-using-0n-complexity-using-onl-krzf | \n\t class Solution {\n\t \n public:\n\t\t\n TreeNode solve(vector &preorder , long mini , long maxi , int &i){\n\t\n if(i>=preorder.size()){\ | adityagarg217 | NORMAL | 2023-06-07T20:13:59.825367+00:00 | 2023-06-07T20:14:53.177920+00:00 | 1,361 | false | \n\t class Solution {\n\t \n public:\n\t\t\n TreeNode* solve(vector<int> &preorder , long mini , long maxi , int &i){\n\t\n if(i>=preorder.size()){\n return NULL ; \n }\n if(preorder[i] < mini || preorder[i] > maxi ){\n return NULL ;\n }\n TreeNode* ne... | 3 | 0 | [] | 0 |
construct-binary-search-tree-from-preorder-traversal | Solution C++ (nlogn) using inorder traversal | solution-c-nlogn-using-inorder-traversal-65ty | \n class Solution {\n\n public:\n \n int posi(vector& inorder , int element , int size){\n\t\n for(int i=0; i preorder,vector\ &inorder | adityagarg217 | NORMAL | 2023-06-07T20:07:22.666449+00:00 | 2023-06-08T14:24:52.835634+00:00 | 2,122 | false | \n class Solution {\n\n public:\n \n int posi(vector<int>& inorder , int element , int size){\n\t\n for(int i=0; i<size ; i++){\n if(inorder[i]==element){\n return i;\n }\n }\n return -1 ;\n }\n TreeNode* solve(vector<int> preorder,vector... | 3 | 0 | [] | 0 |
first-unique-character-in-a-string | Java 7 lines solution 29ms | java-7-lines-solution-29ms-by-zachc-6fal | Hey guys. My solution is pretty straightforward. It takes O(n) and goes through the string twice:\n1) Get the frequency of each character.\n2) Get the first cha | zachc | NORMAL | 2016-08-22T16:53:12.466000+00:00 | 2018-10-25T04:28:35.438130+00:00 | 154,107 | false | Hey guys. My solution is pretty straightforward. It takes O(n) and goes through the string twice:\n1) Get the frequency of each character.\n2) Get the first character that has a frequency of one.\n\nActually the code below passes all the cases. However, according to @xietao0221, we could change the size of the frequenc... | 552 | 10 | [] | 127 |
first-unique-character-in-a-string | C++ 2 solutions | c-2-solutions-by-dr_pro-0fi1 | Brute force solution, traverse string s 2 times. First time, store counts of every character into the hash table, second time, find the first character that app | dr_pro | NORMAL | 2016-08-22T05:58:06.132000+00:00 | 2021-11-10T06:12:57.370038+00:00 | 61,185 | false | Brute force solution, traverse string s 2 times. First time, store counts of every character into the hash table, second time, find the first character that appears only once.\n```\nclass Solution {\npublic:\n int firstUniqChar(string s) {\n unordered_map<char, int> m;\n for (char& c : s) {\n ... | 243 | 7 | [] | 34 |
first-unique-character-in-a-string | Python 3 lines beats 100% (~ 60ms) ! | python-3-lines-beats-100-60ms-by-gregs-3ekj | \n def firstUniqChar(self, s):\n """\n :type s: str\n :rtype: int\n """\n \n letters='abcdefghijklmnopqrstuvwxyz'\n | gregs | NORMAL | 2016-10-30T19:07:39.451000+00:00 | 2018-10-25T00:57:48.893360+00:00 | 66,085 | false | ``` \n def firstUniqChar(self, s):\n """\n :type s: str\n :rtype: int\n """\n \n letters='abcdefghijklmnopqrstuvwxyz'\n index=[s.index(l) for l in letters if s.count(l) == 1]\n return min(index) if len(index) > 0 else -1\n``` | 183 | 18 | [] | 37 |
first-unique-character-in-a-string | JAVA || 100% faster code || Easy Solution with Explanation | java-100-faster-code-easy-solution-with-zlpb4 | \tPLEASE UPVOTE IF YOU LIKE.\n\nclass Solution {\n public int firstUniqChar(String s) {\n // Stores lowest index / first index\n int ans = Int | shivrastogi | NORMAL | 2022-08-16T02:56:45.670698+00:00 | 2022-08-16T02:56:45.670726+00:00 | 34,941 | false | \tPLEASE UPVOTE IF YOU LIKE.\n```\nclass Solution {\n public int firstUniqChar(String s) {\n // Stores lowest index / first index\n int ans = Integer.MAX_VALUE;\n // Iterate from a to z which is 26 which makes it constant\n for(char c=\'a\'; c<=\'z\';c++){\n // indexOf will re... | 182 | 3 | ['Java'] | 16 |
first-unique-character-in-a-string | Java One Pass Solution with LinkedHashMap | java-one-pass-solution-with-linkedhashma-88s4 | LinkedHashMap will not be the fastest answer for this question because the input characters are just from 'a' to 'z', but in other situations it might be faster | nnnomm | NORMAL | 2016-08-24T22:23:13.465000+00:00 | 2018-10-23T02:49:55.412376+00:00 | 41,261 | false | LinkedHashMap will not be the fastest answer for this question because the input characters are just from 'a' to 'z', but in other situations it might be faster than two pass solutions. I post this just for inspiration.\n```\npublic int firstUniqChar(String s) {\n Map<Character, Integer> map = new LinkedHashMap<... | 161 | 4 | [] | 37 |
first-unique-character-in-a-string | JavaScript solution | javascript-solution-by-twjeen-wal3 | \n var firstUniqChar = function(s) {\n for(i=0;i<s.length;i++){\n if (s.indexOf(s[i])===s.lastIndexOf(s[i])){\n return i;\n | twjeen | NORMAL | 2016-08-23T14:16:39.888000+00:00 | 2018-10-15T01:45:50.710524+00:00 | 9,635 | false | \n var firstUniqChar = function(s) {\n for(i=0;i<s.length;i++){\n if (s.indexOf(s[i])===s.lastIndexOf(s[i])){\n return i;\n } \n }\n return -1;\n };\n\nOR hash map method\n\n var firstUniqChar = function(s) \n var map=new Map();\n for(i=0;i<s.le... | 117 | 2 | [] | 15 |
first-unique-character-in-a-string | ✅☑Beats 99% || [C++/Java/Python/JavaScript] || 2 Line Code || EXPLAINED🔥 | beats-99-cjavapythonjavascript-2-line-co-pj6a | PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n\n---\n\n# Approaches\n(Also explained in the code)\n\n1. Initialize an unordered map (mp) to store character counts.\n1. | MarkSPhilip31 | NORMAL | 2024-02-05T01:07:11.909951+00:00 | 2024-02-05T08:05:18.657905+00:00 | 29,798 | false | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n\n---\n\n# Approaches\n(Also explained in the code)\n\n1. Initialize an unordered map (mp) to store character counts.\n1. Iterate through the ... | 108 | 5 | ['Hash Table', 'String', 'Queue', 'Counting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 12 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.