Title: #536 - Construct Binary Tree from String - LeetCode JavaScript Solutions
Open Graph Title: #536 - Construct Binary Tree from String - LeetCode JavaScript Solutions
X Title: #536 - Construct Binary Tree from String - LeetCode JavaScript Solutions
Description: You need to construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It conta...
Open Graph Description: You need to construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It conta...
X Description: You need to construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It conta...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/construct-binary-tree-from-string
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/construct-binary-tree-from-string","name":"#536 - Construct Binary Tree from String - LeetCode JavaScript Solutions","description":"You need to construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It conta...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n * this.val = (val===undefined ? 0 : val)\n * this.left = (left===undefined ? null : left)\n * this.right = (right===undefined ? null : right)\n * }\n */\n/**\n * @param {string} s\n * @return {TreeNode}\n */\nvar str2tree = function(s) {\n if (!s) return null;\n let index = 0;\n return constructTree();\n\n function parseNumber() {\n const isNegative = s[index] === '-';\n if (isNegative) index++;\n\n let num = 0;\n while (index < s.length && /[0-9]/.test(s[index])) {\n num = num * 10 + parseInt(s[index]);\n index++;\n }\n\n return isNegative ? -num : num;\n }\n\n function constructTree() {\n if (index >= s.length) return null;\n\n const value = parseNumber();\n const node = new TreeNode(value);\n if (index < s.length && s[index] === '(') {\n index++;\n node.left = constructTree();\n index++;\n }\n\n if (index < s.length && s[index] === '(') {\n index++;\n node.right = constructTree();\n index++;\n }\n\n return node;\n }\n};","keywords":"LeetCode 536, #536 - Construct Binary Tree from String, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/construct-binary-tree-from-string |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/construct-binary-tree-from-string/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0536-construct-binary-tree-from-string.js |
| String | https://leetcodejavascript.com/tags/string |
| Depth-First Search | https://leetcodejavascript.com/tags/depth-first-search |
| Tree | https://leetcodejavascript.com/tags/tree |
| Binary Tree | https://leetcodejavascript.com/tags/binary-tree |
| Stack | https://leetcodejavascript.com/tags/stack |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow