Title: #606 - Construct String from Binary Tree - LeetCode JavaScript Solutions
Open Graph Title: #606 - Construct String from Binary Tree - LeetCode JavaScript Solutions
X Title: #606 - Construct String from Binary Tree - LeetCode JavaScript Solutions
Description: You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs...
Open Graph Description: You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs...
X Description: You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/construct-string-from-binary-tree
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/construct-string-from-binary-tree","name":"#606 - Construct String from Binary Tree - LeetCode JavaScript Solutions","description":"You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\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 {TreeNode} t\n * @return {string}\n */\nvar tree2str = function(t) {\n return !t ? '' : `${t.val}(${tree2str(t.left)})(${tree2str(t.right)})`.replace(/(\\(\\)){2}|\\(\\)(?=$|\\))/g, '');\n};\n\n\n// alternative, longer but faster version:\n/**\n * @param {TreeNode} t\n * @return {string}\n */\nvar tree2str = function(t) {\n let str = t ? `${t.val}` : '';\n if (t && t.right) str += `(${tree2str(t.left)})(${tree2str(t.right)})`;\n else if (t && t.left) str += `(${tree2str(t.left)})`;\n return str;\n};","keywords":"LeetCode 606, #606 - Construct String from Binary Tree, Easy, 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-string-from-binary-tree |
| 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-string-from-binary-tree/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0606-construct-string-from-binary-tree.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 |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow