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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
longest-substring-of-all-vowels-in-order | Easy C++ Soln || Sliding Window | easy-c-soln-sliding-window-by-mitedyna-cotw | \nclass Solution {\npublic:\n int longestBeautifulSubstring(string w) {\n unordered_map<char,int> mp;\n unordered_map<int,int> cnt;\n mp | mitedyna | NORMAL | 2021-05-26T18:59:47.408043+00:00 | 2021-05-26T18:59:47.408077+00:00 | 236 | false | ```\nclass Solution {\npublic:\n int longestBeautifulSubstring(string w) {\n unordered_map<char,int> mp;\n unordered_map<int,int> cnt;\n mp[\'a\']=0;mp[\'e\']=1;mp[\'i\']=2;mp[\'o\']=3;mp[\'u\']=4;\n int i=0,j=0,n=w.length(),ans=0;\n while(i<n){\n int x=mp[w[i]];\n ... | 1 | 0 | ['C', 'Sliding Window'] | 0 |
longest-substring-of-all-vowels-in-order | Faster than 93% of C# submissions | Inline comments | O(N) time | faster-than-93-of-c-submissions-inline-c-0bz9 | \npublic class Solution {\n public int LongestBeautifulSubstring(string str) {\n int max = 0, s = 0;\n char last = \'$\';\n\t\t// Store vowel s | abhishek-ks | NORMAL | 2021-05-18T08:36:07.801562+00:00 | 2021-05-18T10:21:28.997802+00:00 | 70 | false | ```\npublic class Solution {\n public int LongestBeautifulSubstring(string str) {\n int max = 0, s = 0;\n char last = \'$\';\n\t\t// Store vowel sequence\n var map = new Dictionary<char, int>() {\n {\'a\',0}, {\'e\',1},{\'i\',2},{\'o\',3},{\'u\',4}\n };\n for(int i=0;i<s... | 1 | 0 | [] | 0 |
longest-substring-of-all-vowels-in-order | c++ - O(n) time and O(1) space, beats 93% | with comments | c-on-time-and-o1-space-beats-93-with-com-93uu | \nclass Solution {\npublic:\n // concept used is sliding window.\n // O(n) time complexity and O(1) space complexity.\n int longestBeautifulSubstring(s | subhajitadhikary | NORMAL | 2021-05-07T15:12:07.590975+00:00 | 2021-05-07T15:14:54.959797+00:00 | 284 | false | ```\nclass Solution {\npublic:\n // concept used is sliding window.\n // O(n) time complexity and O(1) space complexity.\n int longestBeautifulSubstring(string word) {\n int maxlen=0; // this will store the max length of valid substring\n int start=0,end=0; // the pointers to traverse the sl... | 1 | 0 | ['Two Pointers', 'C', 'Sliding Window', 'C++'] | 0 |
longest-substring-of-all-vowels-in-order | Golang simple solution | golang-simple-solution-by-tjucoder-o155 | go\nfunc longestBeautifulSubstring(word string) int {\n lbs := 0\n level := 0\n current := 0\n for _, c := range word {\n switch c {\n | tjucoder | NORMAL | 2021-05-04T18:56:17.809660+00:00 | 2021-05-04T18:56:17.809706+00:00 | 90 | false | ```go\nfunc longestBeautifulSubstring(word string) int {\n lbs := 0\n level := 0\n current := 0\n for _, c := range word {\n switch c {\n case \'a\':\n if level == 1 {\n current++\n } else {\n level = 1\n current = 1\n ... | 1 | 0 | ['Go'] | 0 |
longest-substring-of-all-vowels-in-order | C++ Simple | Easy To Understand | c-simple-easy-to-understand-by-rudraksh2-isyi | \nclass Solution {\npublic:\n int longestBeautifulSubstring(string str) {\n \tint a = str.length();\n\tint vowel = 0;\n\tint r = 0;\n\tint maxi = 0;\n | fakeramsingh | NORMAL | 2021-05-03T09:34:03.424263+00:00 | 2021-05-03T09:34:03.424292+00:00 | 93 | false | ```\nclass Solution {\npublic:\n int longestBeautifulSubstring(string str) {\n \tint a = str.length();\n\tint vowel = 0;\n\tint r = 0;\n\tint maxi = 0;\n\tchar last = \'a\';\n\tfor (int i = 0; i < a; i++) {\n\t\tif (str[i] == \'a\') {\n\t\t\tr = i;\n vowel++;\n\t\t\tbreak;\n\t\t}\n\t}\n if (... | 1 | 1 | [] | 0 |
longest-substring-of-all-vowels-in-order | Python | Clean Code | python-clean-code-by-morningstar317-yb13 | ```\nclass Solution:\n def longestBeautifulSubstring(self, word: str) -> int:\n i = 0\n j = 0\n n = len(word)\n maxVal = 0\n | morningstar317 | NORMAL | 2021-04-29T05:47:19.509564+00:00 | 2021-04-29T05:47:19.509603+00:00 | 93 | false | ```\nclass Solution:\n def longestBeautifulSubstring(self, word: str) -> int:\n i = 0\n j = 0\n n = len(word)\n maxVal = 0\n res = set()\n while j < n:\n k = j\n while k < n and word[k]==word[j]:\n res.add(word[k])\n k+=1\n... | 1 | 0 | [] | 0 |
longest-substring-of-all-vowels-in-order | [Java] O(N) sliding window | 18ms | java-on-sliding-window-18ms-by-travis031-gj5i | \nclass Solution {\n public int longestBeautifulSubstring(String word) {\n int maxLen = 0;\n for (int i = 0; i < word.length();) {\n | travis0315 | NORMAL | 2021-04-27T05:06:04.344164+00:00 | 2021-04-27T05:06:04.344223+00:00 | 104 | false | ```\nclass Solution {\n public int longestBeautifulSubstring(String word) {\n int maxLen = 0;\n for (int i = 0; i < word.length();) {\n int start = i, cnt = 1, cur = word.charAt(i);\n while (i < word.length() && cur <= word.charAt(i)) {\n if (cur < word.charAt(i)) {... | 1 | 0 | ['Sliding Window', 'Java'] | 0 |
longest-substring-of-all-vowels-in-order | javascript | javascript-by-mukundjha-p2rx | \nhere i used sliding window approach\n\ni will use two pointers slow and fast.\nstack to keep track of last encountered vowel.\nand also a set to keep watch if | mukundjha | NORMAL | 2021-04-26T16:47:25.794273+00:00 | 2021-04-26T16:47:25.794309+00:00 | 296 | false | ```\nhere i used sliding window approach\n\ni will use two pointers slow and fast.\nstack to keep track of last encountered vowel.\nand also a set to keep watch if set has all 5 vowels(which means we have a valid window)\nmap each vowel in increamenting order.\n\nnow start moving fast pointer\n\nthere are 3 things poss... | 1 | 0 | ['Sliding Window', 'JavaScript'] | 0 |
longest-substring-of-all-vowels-in-order | C# Easy Sliding Window | c-easy-sliding-window-by-venki07-fc7v | \npublic class Solution {\n public int LongestBeautifulSubstring(string word) {\n \n var set = new HashSet<char>();\n int start = 0; | venki07 | NORMAL | 2021-04-25T22:15:18.828292+00:00 | 2021-04-25T22:15:18.828325+00:00 | 43 | false | ```\npublic class Solution {\n public int LongestBeautifulSubstring(string word) {\n \n var set = new HashSet<char>();\n int start = 0; \n int max = 0;\n \n for(int end = 0; end < word.Length; end++)\n {\n if(end > 0 && word[end] < word[end -1])\n ... | 1 | 0 | [] | 1 |
longest-substring-of-all-vowels-in-order | [Python3] Simple One Pass Greedy Solution | python3-simple-one-pass-greedy-solution-tha4a | \nclass Solution:\n def longestBeautifulSubstring(self, word: str) -> int:\n vowels = {\'a\' : 0, \'e\' : 1, \'i\' : 2, \'o\' : 3, \'u\' : 4}\n | blackspinner | NORMAL | 2021-04-25T19:07:02.311911+00:00 | 2021-04-25T19:07:02.311946+00:00 | 30 | false | ```\nclass Solution:\n def longestBeautifulSubstring(self, word: str) -> int:\n vowels = {\'a\' : 0, \'e\' : 1, \'i\' : 2, \'o\' : 3, \'u\' : 4}\n n = len(word)\n last = \'#\'\n j = 0\n res = 0\n for i in range(n):\n if word[i] in vowels:\n if last ... | 1 | 0 | [] | 0 |
longest-substring-of-all-vowels-in-order | TLE || simple ans using regex | tle-simple-ans-using-regex-by-kondekarsh-3ipb | This solution exceeds time\n\nplease suggest me where it goes wrong\n\n\nimport re\nclass Solution:\n def longestBeautifulSubstring(self, word):\n dat | kondekarshubham123 | NORMAL | 2021-04-25T07:35:57.603811+00:00 | 2021-04-25T07:42:07.085118+00:00 | 41 | false | This solution exceeds time\n\nplease suggest me where it goes wrong\n\n```\nimport re\nclass Solution:\n def longestBeautifulSubstring(self, word):\n data = re.findall(\'[a]+[e]+[i]+[o]+[u]+\',word)\n return max(map(len,data)) if data else 0\n``` | 1 | 0 | ['Python3'] | 0 |
longest-substring-of-all-vowels-in-order | Very Simple and Descriptive C++ code | very-simple-and-descriptive-c-code-by-te-vidt | \nclass Solution {\npublic:\n int longestBeautifulSubstring(string s) {\n int n=s.size();\n map<char,int> mp { {\'a\',0},{\'e\',1}, {\'i\',2}, { | tejpratapp468 | NORMAL | 2021-04-25T06:49:34.969463+00:00 | 2021-04-25T06:49:34.969505+00:00 | 53 | false | ```\nclass Solution {\npublic:\n int longestBeautifulSubstring(string s) {\n int n=s.size();\n map<char,int> mp { {\'a\',0},{\'e\',1}, {\'i\',2}, {\'o\',3}, {\'u\',4} };\n int ans=0,cnt=0;\n vector<int> v(5,0);v[mp[s[0]]]++,cnt++;\n for(int i=1;i<n;i++)\n {\n if(mp... | 1 | 0 | [] | 0 |
longest-substring-of-all-vowels-in-order | javascript sliding window 100ms | javascript-sliding-window-100ms-by-henry-kfzq | \nconst mx = Math.max;\nconst t = \'aeiou\';\nconst longestBeautifulSubstring = (s) => {\n let n = s.length;\n let res = 0;\n for (let i = 0; i < n; i+ | henrychen222 | NORMAL | 2021-04-25T05:31:39.062240+00:00 | 2021-04-25T05:38:31.030333+00:00 | 375 | false | ```\nconst mx = Math.max;\nconst t = \'aeiou\';\nconst longestBeautifulSubstring = (s) => {\n let n = s.length;\n let res = 0;\n for (let i = 0; i < n; i++) {\n if (s[i] == \'a\') { // if want the longest, so have to keep as much as "a" comes left of "eiou", so the answer should start with "a"\n ... | 1 | 0 | ['Sliding Window', 'JavaScript'] | 0 |
serialize-and-deserialize-bst | the General Solution for Serialize and Deserialize BST and Serialize and Deserialize BT | the-general-solution-for-serialize-and-d-0rp1 | BST\nuse upper and lower boundaries to check whether we should add null\njava\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * i | greatlim | NORMAL | 2018-10-04T20:28:05.849722+00:00 | 2018-10-17T07:25:29.308896+00:00 | 46,597 | false | ### BST\nuse upper and lower boundaries to check whether we should add `null`\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) { val = x; }\n * }\n */\npublic class Codec {\n\n // Encodes a tree t... | 501 | 0 | [] | 37 |
serialize-and-deserialize-bst | Python O( N ) solution. easy to understand | python-o-n-solution-easy-to-understand-b-bdrh | EDIT: Thanks to @WKVictor , this solution uses 'deque' instead of 'list' as queue. And the performance is O( N )\n\n\nclass Codec:\n\n def serialize(self, ro | leetcodeftw | NORMAL | 2016-11-05T16:15:34.918000+00:00 | 2018-10-26T15:26:53.018142+00:00 | 18,547 | false | EDIT: Thanks to @WKVictor , this solution uses 'deque' instead of 'list' as queue. And the performance is O( N )\n\n```\nclass Codec:\n\n def serialize(self, root):\n vals = []\n\n def preOrder(node):\n if node:\n vals.append(node.val)\n preOrder(node.left)\n ... | 160 | 0 | [] | 15 |
serialize-and-deserialize-bst | Java PreOrder + Queue solution | java-preorder-queue-solution-by-magicshi-5jsh | Hi all, I think my solution is pretty straightforward and easy to understand, not that efficient though. And the serialized tree is compact.\nPre order traversa | magicshine | NORMAL | 2016-11-06T22:39:03.499000+00:00 | 2018-10-21T01:01:25.603021+00:00 | 68,390 | false | Hi all, I think my solution is pretty straightforward and easy to understand, not that efficient though. And the serialized tree is compact.\nPre order traversal of BST will output root node first, then left children, then right.\n```\nroot left1 left2 leftX right1 rightX\n```\nIf we look at the value of the pre-order ... | 154 | 7 | [] | 41 |
serialize-and-deserialize-bst | Concise C++ 19ms solution beating 99.4% | concise-c-19ms-solution-beating-994-by-c-b5ml | Sharing my solution which doesn't have to parse string for comma at all!\n\nThe encoding schema is preorder of BST, and to decode this we can use the same preor | code9yitati | NORMAL | 2017-02-19T07:06:38.673000+00:00 | 2018-10-24T04:46:45.509883+00:00 | 25,414 | false | Sharing my solution which doesn't have to parse string for comma at all!\n\nThe encoding schema is preorder of BST, and to decode this we can use the same preorder traversal to do it in one pass with recursion in O(n) time.\n\nTo minimize the memory, I used binary format instead of ascii format for each integer, just b... | 141 | 4 | [] | 28 |
serialize-and-deserialize-bst | C++ simple and clean PreOrder traversal solution | c-simple-and-clean-preorder-traversal-so-zqvz | \nclass Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n return encode(root);\n }\n\n // Dec | yehudisk | NORMAL | 2020-10-09T11:03:26.240085+00:00 | 2020-10-09T11:03:26.240130+00:00 | 7,811 | false | ```\nclass Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n return encode(root);\n }\n\n // Decodes your encoded data to tree.\n TreeNode* deserialize(string data) {\n stringstream ss(data);\n string item;\n TreeNode* root = NUL... | 92 | 5 | ['C'] | 9 |
serialize-and-deserialize-bst | Deserialize from preorder and computed inorder, reusing old solution | deserialize-from-preorder-and-computed-i-m2pk | I serialize the tree's values in preorder. For deserializing, I additionally compute inorder simply by sorting the preorder. And then I just use the buildTree f | stefanpochmann | NORMAL | 2016-11-04T14:35:12.575000+00:00 | 2018-10-17T05:58:02.054287+00:00 | 30,980 | false | I serialize the tree's values in preorder. For deserializing, I additionally compute inorder simply by sorting the preorder. And then I just use the `buildTree` function that I copied&pasted from [my old solution](https://discuss.leetcode.com/topic/16221/simple-o-n-without-map) for the old problem [Construct Binary Tre... | 78 | 2 | [] | 22 |
serialize-and-deserialize-bst | BT & BST C++ Solution: Preorder, Comma seperated with comments | bt-bst-c-solution-preorder-comma-seperat-hera | (1) Binary Search Tree\n\nclass Codec {\npublic:\n void serializehelper(TreeNode* root, string& s){\n if(root==nullptr) return;\n \n s+= | ankurharitosh | NORMAL | 2020-03-24T10:49:28.839828+00:00 | 2020-08-28T14:00:44.307811+00:00 | 5,308 | false | (1) Binary Search Tree\n```\nclass Codec {\npublic:\n void serializehelper(TreeNode* root, string& s){\n if(root==nullptr) return;\n \n s+=to_string(root->val) + ","; // \',\' for seperating each value\n serializehelper(root->left, s);\n serializehelper(root->right, s);\n }\n ... | 45 | 0 | ['Binary Search Tree', 'C', 'C++'] | 4 |
serialize-and-deserialize-bst | Using lower bound and upper bound to deserialize BST | using-lower-bound-and-upper-bound-to-des-pvh6 | The idea is to encode every non null root value by preorder traversal \n\nwhen deserializing the tree, we can pass by the lower bound and upper bound to know th | cccrrryyy | NORMAL | 2016-11-06T01:24:15.100000+00:00 | 2018-10-24T05:09:51.472616+00:00 | 14,830 | false | The idea is to encode every non null root value by preorder traversal \n\nwhen deserializing the tree, we can pass by the lower bound and upper bound to know the boundary of a subtree.\n\nThis approach has an overhead of looking up one extra number, which would be O ( 2N )\n\n```\n // Encodes a tree to a single stri... | 29 | 1 | [] | 9 |
serialize-and-deserialize-bst | Java concise Solution. | java-concise-solution-by-yizeliu-auqz | \npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if(root == null){\n return | yizeliu | NORMAL | 2018-08-29T08:00:13.037983+00:00 | 2018-10-25T03:00:31.303029+00:00 | 3,720 | false | ```\npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if(root == null){\n return "#";\n }\n return String.valueOf(root.val) + "," + serialize(root.left) + "," + serialize(root.right);\n }\n\n // Decodes your encoded d... | 28 | 1 | [] | 6 |
serialize-and-deserialize-bst | [Python] O(n) solution, using preorder traversal, explained | python-on-solution-using-preorder-traver-6aj5 | My idea to solve this problem is to use Problem 1008. Construct Binary Search Tree from Preorder Traversal. So:\n\n1. For our serialization we just get preorder | dbabichev | NORMAL | 2020-10-09T10:17:38.550108+00:00 | 2020-10-09T10:28:28.908874+00:00 | 2,248 | false | My idea to solve this problem is to use **Problem 1008**. Construct Binary Search Tree from Preorder Traversal. So:\n\n1. For our serialization we just get preorder traversel (iterative or recursion, whatever you want, I used iterative, using stack).\n2. For deserialization we use solution with `O(n)` time complexity: ... | 26 | 0 | [] | 2 |
serialize-and-deserialize-bst | Python, BFS > 90% | python-bfs-90-by-warmr0bot-nqdb | \nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n """\n if not root: return \'\'\ | warmr0bot | NORMAL | 2020-10-09T09:41:15.658203+00:00 | 2020-10-09T09:41:15.658247+00:00 | 2,938 | false | ```\nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n """\n if not root: return \'\'\n tree = []\n queue = deque([root])\n while queue:\n node = queue.popleft()\n if node:\n tree.appen... | 24 | 0 | ['Breadth-First Search', 'Python', 'Python3'] | 3 |
serialize-and-deserialize-bst | Python O(n) sol by pre-order traversal. 75%+ [ With explanation ] | python-on-sol-by-pre-order-traversal-75-d5l3r | Python O(n) sol by pre-order traversal.\n\n---\n\nHint:\n1. Pre-order traversal of binary search tree is unique and only. (i.e., no repetition between two diffe | brianchiang_tw | NORMAL | 2020-02-08T12:40:12.271633+00:00 | 2020-02-10T05:49:40.924891+00:00 | 1,855 | false | Python O(n) sol by pre-order traversal.\n\n---\n\nHint:\n1. Pre-order traversal of binary search tree is **unique and only**. (i.e., no repetition between two different BSTs)\n\n2. **Pre-order traversal rule** of binary search tree is ( **current node, left-child, right-child**)\n\n3. Left child value < current node va... | 24 | 1 | ['String', 'Python'] | 2 |
serialize-and-deserialize-bst | pre or post order with only keeping one bound(beat 98% and 95%) | pre-or-post-order-with-only-keeping-one-peby3 | the post-order:\n\n'''\n\npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if (root == n | ruby71 | NORMAL | 2017-08-01T17:52:07.450000+00:00 | 2018-10-19T22:23:44.947870+00:00 | 4,339 | false | the post-order:\n\n'''\n\npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if (root == null) {\n return null;\n }\n StringBuilder sb = new StringBuilder();\n serialize(root, sb);\n return sb.toString();\n }... | 24 | 0 | [] | 8 |
serialize-and-deserialize-bst | Java O(n) recursive DFS without "null" Changed from Serialize and Deserialize BT | java-on-recursive-dfs-without-null-chang-9y5t | Thanks to this post, I realize that I can make use of lower and upper bound.\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode | youshan | NORMAL | 2016-11-08T00:30:43.825000+00:00 | 2018-09-16T19:37:01.148225+00:00 | 8,371 | false | Thanks to [this post](https://discuss.leetcode.com/topic/66495/using-lower-bound-and-upper-bound-to-deserialize-bst), I realize that I can make use of lower and upper bound.\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) { // preorder\n StringBuilder sb = new StringBuild... | 22 | 1 | [] | 2 |
serialize-and-deserialize-bst | Easy To Understand C++ Solution Using PreOrder Traversal and iostringstream | easy-to-understand-c-solution-using-preo-p517 | Because of BST, so we can use PreOrder Traversal to rebuild the tree, so do with serialize and deserialize. After using iostringstream, we can easily get the va | haoyuanliu | NORMAL | 2017-01-12T02:55:24.698000+00:00 | 2018-10-19T20:49:29.653345+00:00 | 2,778 | false | Because of BST, so we can use `PreOrder Traversal` to rebuild the tree, so do with serialize and deserialize. After using `iostringstream`, we can easily get the values and build the tree.\n\n```\nclass Codec \n{\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) \n {\n ... | 18 | 0 | [] | 5 |
serialize-and-deserialize-bst | Python solution | python-solution-by-zitaowang-1g8d | One can use the encoding and decoding scheme in 297. Serialize and Deserialize Binary Tree. However, it does not use the properties of a BST: The left (right) s | zitaowang | NORMAL | 2019-01-01T01:03:27.795811+00:00 | 2019-01-01T01:03:27.795864+00:00 | 1,598 | false | One can use the encoding and decoding scheme in 297. Serialize and Deserialize Binary Tree. However, it does not use the properties of a BST: The left (right) subtree of a node contains only nodes with keys smaller (greater) than the node\'s key. Therefore, the encoding strings there does not satisfy the requirement th... | 16 | 0 | [] | 5 |
serialize-and-deserialize-bst | C++ Super Simple, Clean & Short Soluion only 11 lines! | c-super-simple-clean-short-soluion-only-j6n2c | \nclass Codec {\npublic:\n\n string serialize(TreeNode* root) \n {\n return !root ? " null" : " " + to_string(root->val) + serialize(root->left) + | debbiealter | NORMAL | 2020-10-09T08:52:43.290140+00:00 | 2020-10-09T09:26:31.363958+00:00 | 1,233 | false | ```\nclass Codec {\npublic:\n\n string serialize(TreeNode* root) \n {\n return !root ? " null" : " " + to_string(root->val) + serialize(root->left) + serialize(root->right);\n }\n\t\n TreeNode* deserialize(string data) \n {\n istringstream ss(data);\n\t\treturn buildBST(ss);\n }\n \np... | 13 | 3 | ['C'] | 1 |
serialize-and-deserialize-bst | Easy Java Solution || 2 Approaches Beats 100% online submissions | easy-java-solution-2-approaches-beats-10-8goc | Approach\n1. Perform DFS and store the result in a string seperated by \',\' and null is represented by \'x\'\n2. To helpdeserialize follow the approach to buil | Viraj_Patil_092 | NORMAL | 2023-03-18T17:22:31.899513+00:00 | 2023-03-18T17:22:55.174906+00:00 | 2,143 | false | # Approach\n1. **Perform DFS and store the result in a string seperated by \',\' and null is represented by \'x\'**\n2. **To helpdeserialize follow the approach to build a Tree**\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N) \n\n- Space complexity:\n<!-- Add your space... | 12 | 0 | ['Tree', 'Depth-First Search', 'Binary Search Tree', 'Binary Tree', 'Java'] | 4 |
serialize-and-deserialize-bst | Small and Simple Code in C++ with O(N) time complexity | small-and-simple-code-in-c-with-on-time-p5ya6 | Do the dry run to understand\nexample:\n1 ( 2 (4) (5) ) (3 (6) ( 7 (8) (9) ) )\n1 -> children 2 and 3\n2 -> children 4 and 5\n3 -> children 6 and 7\n7 -> childr | brickcommander | NORMAL | 2022-03-05T13:41:14.794491+00:00 | 2022-10-05T11:35:53.368706+00:00 | 1,346 | false | Do the dry run to understand\nexample:\n1 ( 2 (4) (5) ) (3 (6) ( 7 (8) (9) ) )\n1 -> children 2 and 3\n2 -> children 4 and 5\n3 -> children 6 and 7\n7 -> children 8 and 9\n```\nclass Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n string str = "";\n ... | 12 | 0 | ['Depth-First Search', 'Binary Search Tree', 'Recursion', 'C', 'C++'] | 4 |
serialize-and-deserialize-bst | [Python] PreOrder Traversal - Clean & Concise - O(N) | python-preorder-traversal-clean-concise-by6of | Same with problem: 297. Serialize and Deserialize Binary Tree\n\npython\nclass Codec:\n def serialize(self, root):\n if root == None:\n ret | hiepit | NORMAL | 2020-10-09T07:32:23.104395+00:00 | 2020-10-09T08:43:05.466969+00:00 | 761 | false | Same with problem: [297. Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)\n\n```python\nclass Codec:\n def serialize(self, root):\n if root == None:\n return "$"\n return str(root.val) + "," + self.serialize(root.left) + "," + self.... | 12 | 1 | [] | 1 |
serialize-and-deserialize-bst | Extremely Elegant and Simple JavaScript solution | extremely-elegant-and-simple-javascript-fkepd | My serialized list takes the form of: \n[root, <elements smaller than root>, <elements bigger than root>] and this is true recursively. I cheated a little bit b | yangshun | NORMAL | 2017-06-22T15:20:21.162000+00:00 | 2017-06-22T15:20:21.162000+00:00 | 916 | false | My serialized list takes the form of: \n`[root, <elements smaller than root>, <elements bigger than root>]` and this is true recursively. I cheated a little bit by using `JSON.stringify` and `JSON.parse` but the general idea is there.\n\n```\nvar serialize = function(root) {\n function traverse(node) {\n if (... | 11 | 2 | [] | 0 |
serialize-and-deserialize-bst | simple javascript solution | simple-javascript-solution-by-osdevisnot-o6fj | \nlet serialize = (root, result = []) => {\n if (root) {\n result.push(root.val);\n result.push(...serialize(root.left));\n result.push(...serialize(r | osdevisnot | NORMAL | 2020-05-01T00:20:14.139975+00:00 | 2020-05-01T00:20:14.140011+00:00 | 979 | false | ```\nlet serialize = (root, result = []) => {\n if (root) {\n result.push(root.val);\n result.push(...serialize(root.left));\n result.push(...serialize(root.right));\n } else {\n result.push(null);\n }\n return result;\n};\n\nlet deserialize = (data) => {\n let val = data.shift();\n if (val == null) r... | 9 | 1 | ['JavaScript'] | 3 |
serialize-and-deserialize-bst | C++ Level-Order Serialization | c-level-order-serialization-by-votrubac-ckn2 | We can use a queue serialize the tree level-by-level. If a child node is null, we just add a separator (instead of null or something to save space).\n\nFor exam | votrubac | NORMAL | 2020-03-20T08:44:48.727114+00:00 | 2020-03-20T08:50:13.654217+00:00 | 697 | false | We can use a queue serialize the tree level-by-level. If a child node is null, we just add a separator (instead of `null` or something to save space).\n\nFor example, this tree `[5,1,7,null,3,6,8,2,null,null]` will be serialized as `5,1,7,,3,6,8,2`.\n\n```cpp\nstring serialize(TreeNode* root) {\n string res;\n ve... | 9 | 1 | [] | 0 |
serialize-and-deserialize-bst | Serialize and Deserialize BST | Python | serialize-and-deserialize-bst-python-by-utgy5 | \ndef serialize(self, root: TreeNode) -> str:\n vals = []\n\n def preOrder(node):\n if node:\n vals.append(node.val)\n | status201 | NORMAL | 2020-10-09T12:46:33.923881+00:00 | 2020-10-09T12:46:33.924018+00:00 | 466 | false | ```\ndef serialize(self, root: TreeNode) -> str:\n vals = []\n\n def preOrder(node):\n if node:\n vals.append(node.val)\n preOrder(node.left)\n preOrder(node.right)\n\n preOrder(root)\n\n return \' \'.join(map(str, vals))\n \n ... | 8 | 0 | [] | 2 |
serialize-and-deserialize-bst | Python Preorder + Deque Solution. Beats 98% | python-preorder-deque-solution-beats-98-p9c30 | ```\n\tdef serialize(self, root):\n if not root:\n return "X,"\n return str(root.val) + "," + self.serialize(root.left) + self.serializ | probuddho | NORMAL | 2019-11-12T02:38:27.456135+00:00 | 2019-11-12T02:38:27.456173+00:00 | 392 | false | ```\n\tdef serialize(self, root):\n if not root:\n return "X,"\n return str(root.val) + "," + self.serialize(root.left) + self.serialize(root.right)\n\n def deserialize(self, data):\n queue = collections.deque(data.split(\',\'))\n return self.deserializeHelper(queue)\n \n ... | 8 | 0 | [] | 2 |
serialize-and-deserialize-bst | Two Approaches || 20ms easy || 0ms object oriented | two-approaches-20ms-easy-0ms-object-orie-t02l | Solution 1:\n\npublic class Codec { // 20ms solution\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if(root== | Lil_ToeTurtle | NORMAL | 2022-08-11T15:42:10.261379+00:00 | 2022-08-11T15:45:20.117708+00:00 | 2,035 | false | Solution 1:\n```\npublic class Codec { // 20ms solution\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if(root==null)return "#";\n return root.val+" "+serialize(root.left)+" "+serialize(root.right);\n }\n\n // Decodes your encoded data to tree.\n String... | 7 | 0 | ['Java'] | 4 |
serialize-and-deserialize-bst | Using Preorder traversal | using-preorder-traversal-by-18praneeth-rery | \nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n res = []\n \n def dfs(root):\n if not root:\n | 18praneeth | NORMAL | 2021-08-22T19:18:33.377209+00:00 | 2021-08-22T19:18:33.377254+00:00 | 547 | false | ```\nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n res = []\n \n def dfs(root):\n if not root:\n res.append(\'N\')\n return\n \n res.append(str(root.val))\n dfs(root.left)\n dfs(root.right)\n ... | 7 | 0 | ['Depth-First Search', 'Python'] | 2 |
serialize-and-deserialize-bst | Serialize and Deserialize BST | Java | Preorder Traversal | serialize-and-deserialize-bst-java-preor-l5fo | ```\n\npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n \n if(root==null) return | niharikajha | NORMAL | 2020-10-10T03:24:29.603739+00:00 | 2020-10-10T03:24:29.603772+00:00 | 559 | false | ```\n\npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n \n if(root==null) return "";\n \n List<String> data = new ArrayList<>();\n preorderTraversal(root, data);\n \n return String.join(",", data);\n }\n... | 7 | 0 | [] | 0 |
serialize-and-deserialize-bst | [C++] BFS-based Solution Explained,~99% Time, ~5% Memory | c-bfs-based-solution-explained99-time-5-ciztv | I have to admit that I am still not great in all those tricks you can do with x-order traversals and then rebuild stuff using invariants. But I will get there; | ajna | NORMAL | 2020-10-10T01:02:37.745214+00:00 | 2020-10-10T01:11:55.588112+00:00 | 1,185 | false | I have to admit that I am still not great in all those tricks you can do with x-order traversals and then rebuild stuff using invariants. But I will get there; for now, I just wanted to have fun, so I went to solve it my own way, creating an encoding of comma-separated values (similar to how you build trees in LC\'s in... | 6 | 0 | ['Breadth-First Search', 'C', 'C++'] | 0 |
serialize-and-deserialize-bst | Python simple, clean and short solution | python-simple-clean-and-short-solution-b-t7f0 | \nclass Codec:\n def serialize(self, root: TreeNode) -> str:\n def encode(root):\n return "" if not root else str(root.val) + "-" + encode( | yehudisk | NORMAL | 2020-10-09T11:17:23.765032+00:00 | 2020-10-09T11:17:23.765108+00:00 | 423 | false | ```\nclass Codec:\n def serialize(self, root: TreeNode) -> str:\n def encode(root):\n return "" if not root else str(root.val) + "-" + encode(root.left)+encode(root.right)\n\n return encode(root)\n \n\n def deserialize(self, data: str) -> TreeNode:\n def insert(root, val):\n... | 6 | 3 | ['Python'] | 0 |
serialize-and-deserialize-bst | Java Solution with Explanation using an Example for better understanding | Queue | Preorder | java-solution-with-explanation-using-an-din12 | EXPLANATION\n\nTaking Example 1 of the question: BST = [2, 1, 3]\n\n1. SERIALIZE / ENCODE: Use Preorder Traversal to serialize the BST where each node value is | aayush912 | NORMAL | 2020-10-09T07:20:36.980502+00:00 | 2020-10-09T07:34:16.249308+00:00 | 431 | false | **EXPLANATION**\n\nTaking Example 1 of the question: ***BST = [2, 1, 3]***\n\n1. **SERIALIZE / ENCODE**: Use **Preorder Traversal** to serialize the BST where each node value is separated by a comma (**","**). So after serialization of the above tree, we will return a string like this: **2,1,3**.\n\n2. **DESERIALIZE /... | 6 | 1 | [] | 1 |
serialize-and-deserialize-bst | Easy to Understand Javascript Solution, 84ms, beats 91% | easy-to-understand-javascript-solution-8-ctw9 | Serialize: push all nodes\' value into an array in pre-order, and join them with \',\' or any non-numeric character\n\nvar serialize = function(root) {\n if | jiayuandeng | NORMAL | 2018-12-04T06:48:43.826978+00:00 | 2018-12-04T06:48:43.827019+00:00 | 415 | false | Serialize: push all nodes\' value into an array in pre-order, and join them with \',\' or any non-numeric character\n```\nvar serialize = function(root) {\n if (root === null) return \'\';\n let result = [];\n serializeHelper(root, result);\n return result.join(\',\');\n};\n\nconst serializeHelper = functio... | 6 | 0 | [] | 1 |
serialize-and-deserialize-bst | Golang BFS solution O(n) | golang-bfs-solution-on-by-tuanbieber-jdi6 | \ntype Codec struct {\n}\n\nfunc Constructor() Codec {\n return Codec{}\n}\n\nfunc (this *Codec) serialize(root *TreeNode) string {\n var queue []*TreeNod | tuanbieber | NORMAL | 2022-03-14T05:44:54.585299+00:00 | 2022-03-14T05:44:54.585348+00:00 | 424 | false | ```\ntype Codec struct {\n}\n\nfunc Constructor() Codec {\n return Codec{}\n}\n\nfunc (this *Codec) serialize(root *TreeNode) string {\n var queue []*TreeNode\n\n\tenqueue(&queue, root)\n\n\tvar result string\n\n\tfor len(queue) > 0 {\n\t\tdequeuedElement := dequeue(&queue)\n\n\t\tif dequeuedElement != nil {\n\t\... | 5 | 0 | ['Breadth-First Search', 'Go'] | 1 |
serialize-and-deserialize-bst | Simple C++ Code | Preorder Traversal | Queue | Easy to understand | simple-c-code-preorder-traversal-queue-e-q525 | \nclass Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n if(!root) return "x,";\n string l= | khushboo2001 | NORMAL | 2021-05-08T10:35:52.255160+00:00 | 2021-05-08T10:35:52.255193+00:00 | 476 | false | ```\nclass Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n if(!root) return "x,";\n string l=serialize(root->left);\n string r=serialize(root->right);\n return to_string(root->val)+","+l+r;\n }\n\n // Decodes your encoded data to ... | 5 | 1 | ['Recursion', 'Queue', 'C'] | 0 |
serialize-and-deserialize-bst | Java O(n) Iterative, no nulls | java-on-iterative-no-nulls-by-serdaroqua-w8es | The whole point of this question is to use the BST property to at get rid of nulls in your serialized string (Question #297). Anything less is a suboptimal solu | serdaroquai | NORMAL | 2018-12-23T20:36:57.481681+00:00 | 2018-12-23T20:36:57.481727+00:00 | 533 | false | The whole point of this question is to use the BST property to at get rid of nulls in your serialized string (Question #297). Anything less is a suboptimal solution.\n\n - To Serialize just do an iterative pre-order traversal. (in real world, recursive can stack overflow if BST is unbalanced)\n - To Deserialize:\n\t ... | 5 | 0 | [] | 0 |
serialize-and-deserialize-bst | Java O(N) recursive solution | java-on-recursive-solution-by-yipqiy-pcaw | ```\n// Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if (root == null) {\n return ".";\n }\n | yipqiy | NORMAL | 2016-12-15T06:00:06.545000+00:00 | 2016-12-15T06:00:06.545000+00:00 | 1,912 | false | ```\n// Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if (root == null) {\n return "*.";\n }\n StringBuilder sb = new StringBuilder();\n sb.append(root.val);\n if (root.left == null && root.right == null) {\n sb.append("*.");\... | 5 | 0 | [] | 0 |
serialize-and-deserialize-bst | Preorder serialize and recursive deserialize C++ 26ms | preorder-serialize-and-recursive-deseria-v3b5 | ```\nclass Codec {\npublic:\n //Encodes a tree to a single string.\n void serialize(TreeNode root, vector &val)\n {\n if(root == NULL) return;\n | freeprogrammer | NORMAL | 2017-02-01T15:52:48.892000+00:00 | 2018-09-11T09:28:30.699428+00:00 | 1,914 | false | ```\nclass Codec {\npublic:\n //Encodes a tree to a single string.\n void serialize(TreeNode* root, vector<int> &val)\n {\n if(root == NULL) return;\n val.push_back(root->val);\n serialize(root->left, val);\n serialize(root->right, val);\n }\n\n string serialize(TreeNode* root... | 5 | 0 | [] | 2 |
serialize-and-deserialize-bst | Very simple and easy solution. | very-simple-and-easy-solution-by-srh_abh-zjj2 | \n\n# Code\npython3 []\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = Non | srh_abhay | NORMAL | 2024-09-08T12:10:55.358622+00:00 | 2024-09-08T12:11:48.015604+00:00 | 612 | false | \n\n# Code\n```python3 []\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 Codec:\n\n def serialize(self, root: Optional[TreeNode]) -> str:\n if not root:\n return ""\n ... | 4 | 0 | ['Python3'] | 0 |
serialize-and-deserialize-bst | Simple solution using level order traversal. | simple-solution-using-level-order-traver-qx60 | \n# Complexity\n- Time complexity:O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(N)\n Add your space complexity here, e.g. O(n) \n\n# | ShivamDubey55555 | NORMAL | 2023-03-06T05:25:54.353400+00:00 | 2023-03-06T05:25:54.353428+00:00 | 572 | false | \n# Complexity\n- Time complexity:$$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeN... | 4 | 0 | ['C++'] | 0 |
serialize-and-deserialize-bst | c++ | Preorder Traversal | Easy to understand | c-preorder-traversal-easy-to-understand-btcsi | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : v | akshat0610 | NORMAL | 2022-10-25T17:51:20.098204+00:00 | 2022-10-25T17:51:20.098300+00:00 | 950 | false | ```\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 Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) \n{\... | 4 | 0 | ['Depth-First Search', 'Breadth-First Search', 'Binary Search Tree', 'C', 'C++'] | 1 |
serialize-and-deserialize-bst | c++ | Preorder Traversal | Easy to understand | c-preorder-traversal-easy-to-understand-8kyyq | \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( | venomhighs7 | NORMAL | 2022-10-08T03:53:29.165441+00:00 | 2022-10-08T03:53:29.165483+00:00 | 1,072 | false | \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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Codec {\npublic:\n string serialize(TreeNode* root) {\n return encode(root);\n }\... | 4 | 0 | ['C++'] | 0 |
serialize-and-deserialize-bst | Simple, straightforward Java solution | simple-straightforward-java-solution-by-cihaj | Simple and easy to understand Java solution.\n(Not the most efficient, due to the simple deserialization approach)\n\n\n // since this is a binary search tre | Tim_P | NORMAL | 2021-07-27T08:04:12.154801+00:00 | 2021-07-27T08:04:12.154834+00:00 | 871 | false | Simple and easy to understand Java solution.\n(Not the most efficient, due to the simple deserialization approach)\n\n```\n // since this is a binary search tree, we\'ll just output values in BFS\n // order (when serializind), then add all values to the tree one by one (when deserializing)\n // serialize will ... | 4 | 0 | ['Breadth-First Search', 'Java'] | 1 |
serialize-and-deserialize-bst | Simple C++ Solution, Comment your thoughts: Time and Space O(N) | simple-c-solution-comment-your-thoughts-p25of | \nclass Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n if(!root){\n return "NULL";\n | level7 | NORMAL | 2021-03-05T13:44:56.106427+00:00 | 2021-03-05T13:44:56.106467+00:00 | 415 | false | ```\nclass Codec {\npublic:\n\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n if(!root){\n return "NULL";\n }\n \n string left = serialize(root->left);\n string right = serialize(root->right);\n \n return to_string(root->va... | 4 | 0 | ['C'] | 1 |
serialize-and-deserialize-bst | easy java solution using bst property similiar to validating bst problem | easy-java-solution-using-bst-property-si-t84g | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) | ajithbabu | NORMAL | 2021-03-03T09:13:29.311627+00:00 | 2021-03-03T09:13:29.311660+00:00 | 978 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) { val = x; }\n * }\n */\npublic class Codec {\n\n int serializedTreeIndexPointer;\n public String serialize(TreeNode root) {\n StringBuilder seri... | 4 | 0 | ['Binary Search Tree', 'Java'] | 1 |
serialize-and-deserialize-bst | Python3 Solution Explained (video + code) (inorder + preorder) | python3-solution-explained-video-code-in-cxe8 | \nhttps://www.youtube.com/watch?v=g6Q1cpQvz8A\n\nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\ | spec_he123 | NORMAL | 2020-10-10T05:57:03.952090+00:00 | 2020-10-10T05:57:03.952134+00:00 | 687 | false | [](https://www.youtube.com/watch?v=g6Q1cpQvz8A)\nhttps://www.youtube.com/watch?v=g6Q1cpQvz8A\n```\nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n """\n res = []\n \n def preorder(root):\n if root:\n r... | 4 | 0 | ['Python3'] | 1 |
serialize-and-deserialize-bst | Python [recursion] | python-recursion-by-gsan-9h39 | Serialize the pre-order traversal into a string. Reconstruct from this representation. Both functions can be implemented by recursion.\n\n\nclass Codec:\n de | gsan | NORMAL | 2020-10-09T07:56:10.910638+00:00 | 2020-10-09T07:56:10.910680+00:00 | 291 | false | Serialize the pre-order traversal into a string. Reconstruct from this representation. Both functions can be implemented by recursion.\n\n```\nclass Codec:\n def serialize(self, root: TreeNode) -> str:\n if not root:\n return \'\'\n s1 = self.serialize(root.left)\n s2 = self.serialize... | 4 | 0 | [] | 0 |
serialize-and-deserialize-bst | C : custom low-level data mapping - no strings, no recursion, no built-in DS - simple n short | c-custom-low-level-data-mapping-no-strin-zov7 | Context : This solution of mine uses a custom low-level raw data mapping representaion. There is no string parsing involved, no recursive approach is being used | universalcoder12 | NORMAL | 2020-07-13T00:49:12.827457+00:00 | 2020-07-20T18:10:49.469600+00:00 | 185 | false | **Context :** This solution of mine uses a custom low-level raw data mapping representaion. There is no string parsing involved, no recursive approach is being used, and no built-in Data Structures are used. I have kept this code like all of my other published code as barebone C code as possible.\n\n**Details :** Befor... | 4 | 0 | ['C'] | 0 |
serialize-and-deserialize-bst | Java, bfs, queue | java-bfs-queue-by-chuanqiu-j3vf | \npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n StringBuilder sb = new StringBuilder( | chuanqiu | NORMAL | 2020-05-19T21:12:02.233013+00:00 | 2020-05-19T21:12:02.233058+00:00 | 248 | false | ```\npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n StringBuilder sb = new StringBuilder();\n if(root ==null) return sb.toString();\n Queue<TreeNode> q = new LinkedList<>();\n q.add(root);\n while(!q.isEmpty()){\n ... | 4 | 1 | [] | 1 |
serialize-and-deserialize-bst | 💡JavaScript Solution - Preorder | javascript-solution-preorder-by-aminick-7n7r | The idea\n1. Utilize the fact BST can be reconstructed from preorder/postorder array\njavascript\nvar serialize = function(root) {\n let preorder = [];\n | aminick | NORMAL | 2020-01-31T11:32:48.652350+00:00 | 2020-01-31T11:32:48.652383+00:00 | 666 | false | ### The idea\n1. Utilize the fact BST can be reconstructed from preorder/postorder array\n``` javascript\nvar serialize = function(root) {\n let preorder = [];\n let dfs = function(node) {\n if (node==null) return;\n preorder.push(node.val);\n dfs(node.left);\n dfs(node.right);\n }\... | 4 | 0 | ['JavaScript'] | 1 |
serialize-and-deserialize-bst | Recursive Preorder Python Solution - 20 lines | recursive-preorder-python-solution-20-li-mc4z | ```\nclass Codec:\n \n def serialize(self, root):\n if not root: return\n def serializeTree(node):\n if not node:\n | shaw3257 | NORMAL | 2018-08-31T18:19:19.720632+00:00 | 2018-10-25T19:37:24.064848+00:00 | 774 | false | ```\nclass Codec:\n \n def serialize(self, root):\n if not root: return\n def serializeTree(node):\n if not node:\n return \'null\'\n return str(node.val) + "," + serializeTree(node.left) + "," + serializeTree(node.right)\n return serializeTree(root)\n ... | 4 | 0 | [] | 0 |
serialize-and-deserialize-bst | 2 concise Java Solutions (O(n) and O(n2)) with using BST's feature (Not the old generic solution for Binary Tree) | 2-concise-java-solutions-on-and-on2-with-ffpb | Idea:\nWe are given a BST, so it is possible to reconstruct the tree by using Pre-Order traverse array.\n\nNotice we have 1 requirement "The encoded string shou | nathanni | NORMAL | 2016-12-02T07:43:48.749000+00:00 | 2018-09-21T17:49:23.092219+00:00 | 370 | false | **Idea:**\nWe are given a BST, so it is possible to **reconstruct the tree by using Pre-Order traverse array**.\n\n*Notice we have 1 requirement "The encoded string should be as compact as possible." So let us forget the generic BFS/DFS solution for Binary Tree (Adding "#" or "NULL" for empty node)*\n\n\nSerialize:\nVe... | 4 | 0 | [] | 0 |
serialize-and-deserialize-bst | Simple Python preorder | simple-python-preorder-by-realisking-9h8x | In serialize step we do a preorder traversal to get the string. For reconstructing the BST, l and r are the left and right boundaries of our current array. Sinc | realisking | NORMAL | 2017-04-07T11:56:08.869000+00:00 | 2017-04-07T11:56:08.869000+00:00 | 428 | false | In ```serialize``` step we do a preorder traversal to get the string. For reconstructing the BST, ```l``` and ```r``` are the left and right boundaries of our ```current``` array. Since it's preorder traversal, for root value ```A[l]```, once we find the first point ```A[mid] < A[l]```, we know all values from this poi... | 4 | 0 | [] | 0 |
serialize-and-deserialize-bst | A few solutions | a-few-solutions-by-claytonjwong-y1us | Whitespace delimited serialization/deserialization of the pre-order traversal of the tree. null values are represented as -1.\n\n---\n\nKotlin\n\nclass Codec() | claytonjwong | NORMAL | 2017-07-31T19:53:26.365000+00:00 | 2021-05-26T15:11:44.514429+00:00 | 437 | false | Whitespace delimited serialization/deserialization of the pre-order traversal of the tree. `null` values are represented as `-1`.\n\n---\n\n*Kotlin*\n```\nclass Codec() {\n fun serialize(root: TreeNode?): String {\n var A = mutableListOf<Int>()\n fun go(node: TreeNode? = root) {\n if (node ... | 4 | 2 | [] | 0 |
serialize-and-deserialize-bst | short and clean solution | short-and-clean-solution-by-__abcd-t84r | \nclass Codec {\npublic:\n\n string serialize(TreeNode* root) {\n if(!root)return "#";\n return to_string(root->val)+"#"+serialize(root->left) | __Abcd__ | NORMAL | 2022-10-27T05:07:56.645914+00:00 | 2022-10-27T05:07:56.645961+00:00 | 757 | false | ```\nclass Codec {\npublic:\n\n string serialize(TreeNode* root) {\n if(!root)return "#";\n return to_string(root->val)+"#"+serialize(root->left) + serialize(root->right);\n }\n TreeNode* decode(stringstream& take,string res = ""){\n getline(take,res,\'#\');\n if(res.empty())return ... | 3 | 0 | ['C'] | 0 |
serialize-and-deserialize-bst | Python - DFS || Easy and Simple Solution | python-dfs-easy-and-simple-solution-by-d-wtjf | \nclass Codec:\n def serialize(self, root: Optional[TreeNode]) -> str:\n res = []\n \n def dfs(node):\n if not node:\n | dayaniravi123 | NORMAL | 2022-04-11T14:57:04.293435+00:00 | 2022-04-11T14:57:04.293478+00:00 | 474 | false | ```\nclass Codec:\n def serialize(self, root: Optional[TreeNode]) -> str:\n res = []\n \n def dfs(node):\n if not node:\n res.append("N")\n return\n res.append(str(node.val))\n dfs(node.left)\n dfs(node.right)\n ... | 3 | 0 | ['Depth-First Search', 'Python3'] | 0 |
serialize-and-deserialize-bst | [Java] Solution - Level Order Traversal | java-solution-level-order-traversal-by-r-5q4y | Level Order Traversal\nNote that this is not the most compact solution and that this does not use the special property of binary search tree. It\'s however simp | repititionismastery | NORMAL | 2021-04-18T07:16:36.419652+00:00 | 2021-04-18T07:33:29.466645+00:00 | 207 | false | # Level Order Traversal\nNote that this is not the most compact solution and that this does not use the special property of binary search tree. It\'s however simple to implement and does the job.\n\nTo Serialize:\n* We traverse the BST in level order.\n* We use a separator and append the node values to our string. We u... | 3 | 0 | [] | 0 |
serialize-and-deserialize-bst | Serialize and Deserialize BST C++ | serialize-and-deserialize-bst-c-by-razva-nhp5 | \nclass Codec {\npublic:\n\n string serialize(TreeNode* root) \n {\n return !root ? " null" : " " + to_string(root->val) + serialize(root->left) + | razvan1403 | NORMAL | 2020-10-10T06:11:19.015890+00:00 | 2020-10-10T06:11:19.015928+00:00 | 80 | false | ```\nclass Codec {\npublic:\n\n string serialize(TreeNode* root) \n {\n return !root ? " null" : " " + to_string(root->val) + serialize(root->left) + serialize(root->right);\n }\n\t\n TreeNode* deserialize(string data) \n {\n istringstream ss(data);\n\t\treturn buildBST(ss);\n }\n \np... | 3 | 0 | [] | 0 |
serialize-and-deserialize-bst | [Python] simple recursive solution | python-simple-recursive-solution-by-101l-80e1 | \n ## RC ##\n ## APPROACH : RECURSION ##\n ## Similar to 297. Serialize and Deserialize Binary Tree ##\n\t\t## TIME COMPLEXITY : O(N) ##\n\ | 101leetcode | NORMAL | 2020-06-22T15:19:58.104761+00:00 | 2020-06-22T15:19:58.104804+00:00 | 555 | false | ```\n ## RC ##\n ## APPROACH : RECURSION ##\n ## Similar to 297. Serialize and Deserialize Binary Tree ##\n\t\t## TIME COMPLEXITY : O(N) ##\n\t\t## SPACE COMPLEXITY : O(N) ##\n\nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n "... | 3 | 1 | ['Python', 'Python3'] | 1 |
serialize-and-deserialize-bst | javascript preorder | javascript-preorder-by-abdhalees-txm7 | \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/**\n * E | abdhalees | NORMAL | 2020-01-29T01:56:46.209438+00:00 | 2020-01-29T01:56:46.209487+00:00 | 351 | false | ```\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/**\n * Encodes a tree to a single string.\n *\n * @param {TreeNode} root\n * @return {string}\n */\nvar serialize = function(root) {\n if(!root) return \'\'\n retu... | 3 | 0 | ['JavaScript'] | 0 |
serialize-and-deserialize-bst | Easy to follow, Python, using queue | easy-to-follow-python-using-queue-by-ale-o8pd | \n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n """\n if not root: return \'\'\n q = col | alexyalunin | NORMAL | 2020-01-13T21:12:59.637698+00:00 | 2020-01-13T21:12:59.637748+00:00 | 487 | false | ```\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n """\n if not root: return \'\'\n q = collections.deque([root])\n res = []\n while q:\n node = q.popleft()\n if node:\n q.append(node.left)\n ... | 3 | 0 | ['Python'] | 0 |
serialize-and-deserialize-bst | Python PreOrder + Queue solution with comments | python-preorder-queue-solution-with-comm-mrwx | \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 | darshil_parikh | NORMAL | 2019-05-02T06:13:34.824563+00:00 | 2019-05-02T06:13:34.824664+00:00 | 401 | 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 Codec:\n\n def serialize(self, root):\n """Encodes a tree to a single string.\n \n :type root: TreeNode\n ... | 3 | 0 | [] | 1 |
serialize-and-deserialize-bst | A Simple Solution with Only 12 Lines of Code Beats 98% | a-simple-solution-with-only-12-lines-of-qzjgb | This solution utilizes a small array to hold the position of current value, which simplifies the code to the minimum.\n\n\npublic class Codec {\n\n // Encode | lancewang | NORMAL | 2018-07-06T23:54:35.525351+00:00 | 2018-09-12T15:24:52.428666+00:00 | 474 | false | This solution utilizes a small array to hold the position of current value, which simplifies the code to the minimum.\n\n```\npublic class Codec {\n\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n if(root == null) return "#";\n \n return root.val + "," + s... | 3 | 0 | [] | 1 |
serialize-and-deserialize-bst | C++ 28ms soluton, Used the same encoding scheme Leetcode uses to serialize/deserialize tree problem I/O | c-28ms-soluton-used-the-same-encoding-sc-y7cu | Encoding scheme\nFor below tree\n\n 2\n / \\\n 1 4\n / \\\n 3 5\n\nAfter serialization\n\n2,1,4,#,#,3,5\n\n#### Implementation\n\n\n/**\n * Def | kaidul | NORMAL | 2016-11-04T13:24:17.183000+00:00 | 2016-11-04T13:24:17.183000+00:00 | 1,512 | false | #### Encoding scheme\nFor below tree\n```\n 2\n / \\\n 1 4\n / \\\n 3 5\n```\nAfter serialization\n```\n2,1,4,#,#,3,5\n```\n#### Implementation\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... | 3 | 0 | [] | 0 |
serialize-and-deserialize-bst | C# - pre-order serialize - very compact, no commas, no null, no separators | c-pre-order-serialize-very-compact-no-co-2ohv | Here is the idea. Each node can be serialized with a prefix char and and Abs value. The prefix char acts as the separator and denotes the sign of the value an | jdrogin | NORMAL | 2016-11-05T20:43:27.895000+00:00 | 2016-11-05T20:43:27.895000+00:00 | 980 | false | Here is the idea. Each node can be serialized with a prefix char and and Abs value. The prefix char acts as the separator and denotes the sign of the value and if the value has a left or right or both or is a leaf.\n\nI use this code but any similar codes could be used\n```\n'x' = leaf\n'l' = left node only\n'r' = ri... | 3 | 0 | [] | 0 |
serialize-and-deserialize-bst | Easy BFS JAVA | easy-bfs-java-by-jonkugolu-pfig | ```\n// Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n Queue q=new LinkedList();\n StringBuilder s=new StringB | jonkugolu | NORMAL | 2016-12-18T00:06:18.885000+00:00 | 2016-12-18T00:06:18.885000+00:00 | 923 | false | ```\n// Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n Queue<TreeNode> q=new LinkedList<TreeNode>();\n StringBuilder s=new StringBuilder();\n if(root==null)\n return "*-";\n q.add(root);\n while(!q.isEmpty()){\n TreeNode temp=q.rem... | 3 | 0 | [] | 0 |
serialize-and-deserialize-bst | construct BST using preorder traversal. | construct-bst-using-preorder-traversal-b-1opt | At first I thought this was the same as serialization/deserialization of a generic BT. Since the problem suggests minimal codec size and avoid using any externa | shallpion | NORMAL | 2016-11-01T03:51:39.141000+00:00 | 2016-11-01T03:51:39.141000+00:00 | 2,205 | false | At first I thought this was the same as serialization/deserialization of a generic BT. Since the problem suggests minimal codec size and avoid using any external variable/function/etc I chose preorder and using stack to avoid using aux function. It can be made a little easier if using recursion I suppose. Also I think ... | 3 | 0 | [] | 1 |
serialize-and-deserialize-bst | Python solution serializing as Preorder list | python-solution-serializing-as-preorder-v7afd | The remarkable difference from serialize/deserialize Binary Tree ( https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ ) is, BST can be constru | redtree1112 | NORMAL | 2017-07-03T08:22:56.445000+00:00 | 2017-07-03T08:22:56.445000+00:00 | 808 | false | The remarkable difference from serialize/deserialize Binary Tree ( https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ ) is, **BST can be constructed just from its preorder array** while a general binary tree requires both [in-order and and pre-order](https://leetcode.com/problems/construct-binary-tree... | 3 | 0 | ['Python'] | 1 |
serialize-and-deserialize-bst | C++ Truly O(n) solution using BFS with explanation | c-truly-on-solution-using-bfs-with-expla-ry3l | Can also be used to optimally solve https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/\n\nUnlike many solutions posted, this is a | Ningck | NORMAL | 2024-11-12T22:29:18.533081+00:00 | 2024-11-12T22:29:18.533117+00:00 | 175 | false | Can also be used to optimally solve https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/\n\nUnlike many solutions posted, this is a truly O(n) solution. Basically we are seralize and deserializing using BFS or level-by-level traversal.\n\n# Code\n```cpp []\n/**\n * Definition for a binary tr... | 2 | 0 | ['C++'] | 0 |
serialize-and-deserialize-bst | easy solution :) | easy-solution-by-swati_123-a-qmag | 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 | Swati_123-a | NORMAL | 2024-07-17T01:06:16.556058+00:00 | 2024-07-17T01:06:16.556079+00:00 | 345 | 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)$$ --... | 2 | 0 | ['C++'] | 0 |
serialize-and-deserialize-bst | 🌺26. Pre Order Traversal || 2 Solutions|| Short & Sweet Code || C++ Code Reference | 26-pre-order-traversal-2-solutions-short-g557 | Intuition\n1. By Using Any Traversal Technique PreOrder, PostOrder Or Level Order :\n - Convert Tree To String \n - Convert Back String To Tree \n# Approa | Amanzm00 | NORMAL | 2024-07-10T04:04:51.897390+00:00 | 2024-07-10T04:31:18.303233+00:00 | 252 | false | # Intuition\n1. By Using Any Traversal Technique PreOrder, PostOrder Or Level Order :\n - Convert Tree To String \n - Convert Back String To Tree \n# Approach\n1. I Traverse PreOrder Wise To Serialize The Tree Using `Preorder` Function !\n2. In Deserialization I Parse The String & Store Node Values In Vector \n3... | 2 | 0 | ['String', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Design', 'Binary Search Tree', 'Recursion', 'C++'] | 0 |
serialize-and-deserialize-bst | ostringstream || istringstream | ostringstream-istringstream-by-ayushrakw-lsp3 | 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 | ayushrakwal94 | NORMAL | 2024-01-12T13:26:52.107170+00:00 | 2024-01-12T13:26:52.107208+00:00 | 114 | 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)$$ --... | 2 | 0 | ['C++'] | 0 |
serialize-and-deserialize-bst | 3-line code only || 100 % faster java solution | 3-line-code-only-100-faster-java-solutio-4obw | 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 | alokranjanjha10 | NORMAL | 2023-09-11T14:16:54.676175+00:00 | 2023-09-11T14:16:54.676201+00:00 | 348 | 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)$$ --... | 2 | 1 | ['Java'] | 2 |
serialize-and-deserialize-bst | 3 line || 0 ms || 100 % faster java solution | 3-line-0-ms-100-faster-java-solution-by-wa9xq | 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 | bikramghoshal6337 | NORMAL | 2023-09-11T06:57:39.768143+00:00 | 2023-09-11T06:57:39.768177+00:00 | 184 | 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)$$ --... | 2 | 1 | ['Java'] | 1 |
serialize-and-deserialize-bst | ✅✔️Easy Implementation using level order Traversal || C++ Solution ✈️✈️✈️✈️✈️ | easy-implementation-using-level-order-tr-24ui | 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 | ajay_1134 | NORMAL | 2023-07-01T13:34:26.691524+00:00 | 2023-07-01T13:34:26.691554+00:00 | 327 | 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)$$ --... | 2 | 0 | ['Tree', 'Breadth-First Search', 'Binary Search Tree', 'Binary Tree', 'C++'] | 0 |
serialize-and-deserialize-bst | C++ || Serialize & Deserialize || Easy approach | c-serialize-deserialize-easy-approach-by-k3i7 | Here is my c++ code for this problem.\n\n# Complexity\n- Time complexity:O(n)\n\n- Space complexity:O(n)\n\n# Code\n\n/**\n * Definition for a binary tree node. | mrigank_2003 | NORMAL | 2023-03-16T06:01:53.686195+00:00 | 2023-03-16T06:01:53.686226+00:00 | 1,601 | false | Here is my c++ code for this problem.\n\n# Complexity\n- Time complexity:$$O(n)$$\n\n- Space complexity:$$O(n)$$\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(int x) : val(x), left(NULL), right(NULL) {}... | 2 | 0 | ['String', 'Binary Search Tree', 'Binary Tree', 'C++'] | 0 |
serialize-and-deserialize-bst | 449: Time 91.2%, Solution with step by step explanation | 449-time-912-solution-with-step-by-step-buwq5 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nFor the serialize function:\n\n1. If the given root is None, return an em | Marlen09 | NORMAL | 2023-03-09T04:59:39.043422+00:00 | 2023-03-09T04:59:39.043470+00:00 | 2,270 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nFor the serialize function:\n\n1. If the given root is None, return an empty string.\n2. Create an empty stack and push the root node to it.\n3. Create an empty string called serialized.\n4. While the stack is not empty, pop... | 2 | 0 | ['String', 'Tree', 'Depth-First Search', 'Python', 'Python3'] | 1 |
serialize-and-deserialize-bst | Python solution | python-solution-by-obose-hrhy | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is asking to implement a serialization and deserialization method for a bin | Obose | NORMAL | 2023-01-14T20:20:35.112362+00:00 | 2023-01-14T20:20:35.112390+00:00 | 810 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking to implement a serialization and deserialization method for a binary tree. The serialization method should take a binary tree and convert it into a string format, while the deserialization method should take the stri... | 2 | 0 | ['Python3'] | 0 |
serialize-and-deserialize-bst | Python : Upper/Lower Approach [Preorder DFS] ✅ | python-upperlower-approach-preorder-dfs-aittj | \nclass Codec:\n\n def serialize(self, root: Optional[TreeNode]) -> str:\n self.ans = []\n \n def dfs(node):\n if not node: r | khacker | NORMAL | 2022-09-30T06:09:21.544147+00:00 | 2022-09-30T06:09:21.544190+00:00 | 88 | false | ```\nclass Codec:\n\n def serialize(self, root: Optional[TreeNode]) -> str:\n self.ans = []\n \n def dfs(node):\n if not node: return\n self.ans.append(str(node.val))\n dfs(node.left)\n dfs(node.right)\n \n dfs(root)\n return ",".j... | 2 | 0 | ['Depth-First Search', 'Python', 'Python3'] | 1 |
serialize-and-deserialize-bst | Cringiest solution || 0ms || 100% Faster solution || using static | cringiest-solution-0ms-100-faster-soluti-95g7 | \npublic class Codec {\n static TreeNode node;\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n node = root; | Lil_ToeTurtle | NORMAL | 2022-08-11T15:39:59.275408+00:00 | 2022-08-11T15:39:59.275451+00:00 | 485 | false | ```\npublic class Codec {\n static TreeNode node;\n // Encodes a tree to a single string.\n public String serialize(TreeNode root) {\n node = root;\n return "";\n }\n\n // Decodes your encoded data to tree.\n public TreeNode deserialize(String data) {\n return node;\n }\n}\n\n`... | 2 | 1 | ['Java'] | 4 |
serialize-and-deserialize-bst | Java simple preorder solution | java-simple-preorder-solution-by-1605448-9309 | \npublic class Codec {\n\n // Encodes a tree to a single string.\n public void preorder(TreeNode root, StringBuilder sb) {\n if (root == null) {\n | 1605448777 | NORMAL | 2022-07-08T09:58:14.409481+00:00 | 2022-07-08T09:58:14.409523+00:00 | 686 | false | ```\npublic class Codec {\n\n // Encodes a tree to a single string.\n public void preorder(TreeNode root, StringBuilder sb) {\n if (root == null) {\n sb.append("n" + "/");\n return;\n }\n sb.append(root.val + "/");\n preorder(root.left, sb);\n preorder(root... | 2 | 0 | ['Depth-First Search', 'Binary Search Tree', 'Java'] | 0 |
serialize-and-deserialize-bst | Java Level Order Traversal | Readable Code | java-level-order-traversal-readable-code-wnt0 | \npublic class Codec {\n /**\n * Traverses binary tree in level order \n * Stores node values in string(comma separated)\n */\n private String | priyajitbera | NORMAL | 2022-06-26T19:26:13.685956+00:00 | 2022-06-26T19:26:13.685990+00:00 | 551 | false | ```\npublic class Codec {\n /**\n * Traverses binary tree in level order \n * Stores node values in string(comma separated)\n */\n private String levelOrderTrav(TreeNode root){\n StringBuilder sb = new StringBuilder();\n Queue<TreeNode> que = new LinkedList<>();\n que.add(root);\n... | 2 | 0 | ['Java'] | 0 |
serialize-and-deserialize-bst | C++ simple level-order traversal | c-simple-level-order-traversal-by-mayank-xho6 | \n // Encodes a tree to a single string.\n string serialize(TreeNode root) {\n if(root==NULL)\n return "";\n string s="";\n | mayankks0010 | NORMAL | 2022-01-21T13:32:57.203669+00:00 | 2022-01-21T13:32:57.203724+00:00 | 164 | false | \n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n if(root==NULL)\n return "";\n string s="";\n \n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty())\n {\n TreeNode* temp=q.front();\n q.pop();\n ... | 2 | 0 | ['String', 'Queue'] | 1 |
serialize-and-deserialize-bst | Beat 99% 2ms No delimiter(e.g. comma), no stack/queue. Just turn val into char | beat-99-2ms-no-delimitereg-comma-no-stac-q3db | \npublic class Codec {\n\n /*\n max node.val is 10000, less than 6xxxx(max value of char) \n So just turn node.val into char.\n Serialize with pre | cstsangac | NORMAL | 2021-08-13T08:33:10.261815+00:00 | 2021-08-13T08:35:32.536015+00:00 | 281 | false | ```\npublic class Codec {\n\n /*\n max node.val is 10000, less than 6xxxx(max value of char) \n So just turn node.val into char.\n Serialize with preorder (parent > left > right)\n */\n public String serialize(TreeNode root) {\n StringBuilder sb = new StringBuilder();\n helper_serializ... | 2 | 1 | ['Java'] | 0 |
serialize-and-deserialize-bst | C++ | Queue and Preorder | c-queue-and-preorder-by-steffi_keran-8bmu | \nclass Codec {\npublic:\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n if(!root) return "x,";\n string l=s | steffi_keran | NORMAL | 2021-08-08T13:12:50.909584+00:00 | 2021-08-08T13:12:50.909625+00:00 | 268 | false | ```\nclass Codec {\npublic:\n // Encodes a tree to a single string.\n string serialize(TreeNode* root) {\n if(!root) return "x,";\n string l=serialize(root->left);\n string r=serialize(root->right);\n return to_string(root->val)+","+l+r;\n }\n \n TreeNode* deserializeHelper(q... | 2 | 0 | ['Queue', 'C'] | 0 |
serialize-and-deserialize-bst | [Python] Json string | python-json-string-by-oystermax-hca8 | This solution may not be a perfect one due to that fact that json is not "as compact as possible". However, json is quite convineniet when it comes to web. \n\n | oystermax | NORMAL | 2021-07-26T21:08:38.824102+00:00 | 2021-07-26T21:18:29.691335+00:00 | 184 | false | This solution may not be a perfect one due to that fact that json is not "**as compact as possible**". However, json is quite convineniet when it comes to web. \n```\nclass Codec:\n\n def serialize(self, root: TreeNode) -> str:\n """Encodes a tree to a single string.\n """\n\t\t# Converts a TreeNode in... | 2 | 2 | ['Python'] | 0 |
serialize-and-deserialize-bst | C++ | Simple and Intuitive | 99% | Easy to Understand | c-simple-and-intuitive-99-easy-to-unders-ufeq | \nclass Codec {\npublic:\n void encode(TreeNode* root, string &str){\n if(root == NULL)\n return;\n str += to_string(root->val);\n | Debug_Entity | NORMAL | 2021-07-19T18:17:43.557862+00:00 | 2021-07-19T18:17:43.557902+00:00 | 341 | false | ```\nclass Codec {\npublic:\n void encode(TreeNode* root, string &str){\n if(root == NULL)\n return;\n str += to_string(root->val);\n str += "-";\n encode(root->left, str);\n encode(root->right, str);\n }\n\n // Encodes a tree to a single string.\n string serial... | 2 | 0 | ['Binary Search Tree', 'C', 'C++'] | 1 |
serialize-and-deserialize-bst | short ^^ | short-by-andrii_khlevniuk-geuv | Serialize: BFS, time O(N);\nDeserialize: iterative insert into BST, time O(NlogN).\n\nclass Codec \n{\npublic:\n string serialize(TreeNode* r) \n {\n | andrii_khlevniuk | NORMAL | 2021-04-09T13:36:44.937647+00:00 | 2021-04-09T14:02:48.925877+00:00 | 251 | false | **Serialize:** BFS, time `O(N)`;\n**Deserialize:** iterative insert into BST, time `O(NlogN)`.\n```\nclass Codec \n{\npublic:\n string serialize(TreeNode* r) \n {\n string out;\n for(queue<TreeNode*> q({r}); !empty(q); q.pop())\n if(q.front())\n {\n q.push(q.fron... | 2 | 1 | ['C', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.