{"id":3665,"date":"2018-08-22T08:34:25","date_gmt":"2018-08-22T15:34:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3665"},"modified":"2018-08-22T08:44:39","modified_gmt":"2018-08-22T15:44:39","slug":"leetcode-606-construct-string-from-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-606-construct-string-from-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 606. Construct String from Binary Tree"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 606. Construct String from Binary Tree - \u5237\u9898\u627e\u5de5\u4f5c EP18\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/EggWOgUnt2M?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1>Problem<\/h1>\n<p>You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.<\/p>\n<p>The null node needs to be represented by empty parenthesis pair &#8220;()&#8221;. And you need to omit all the empty parenthesis pairs that don&#8217;t affect the one-to-one mapping relationship between the string and the original binary tree.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> Binary tree: [1,2,3,4]\r\n       1\r\n     \/   \\\r\n    2     3\r\n   \/    \r\n  4     \r\n\r\n<b>Output:<\/b> \"1(2(4))(3)\"\r\n\r\n<b>Explanation:<\/b> Originallay it needs to be \"1(2(4)())(3()())\", \r\nbut you need to omit all the unnecessary empty parenthesis pairs. \r\nAnd it will be \"1(2(4))(3)\".\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> Binary tree: [1,2,3,null,4]\r\n       1\r\n     \/   \\\r\n    2     3\r\n     \\  \r\n      4 \r\n\r\n<b>Output:<\/b> \"1(2()(4))(3)\"\r\n\r\n<b>Explanation:<\/b> Almost the same as the first example, \r\nexcept we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.<\/pre>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n^2)<\/p>\n<p>space complexity: O(n^2)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  string tree2str(TreeNode* t) {\r\n    if (!t) return \"\";\r\n    const string s = std::to_string(t-&gt;val);\r\n    \/\/ case 0: s\r\n    if (!t-&gt;left &amp;&amp; !t-&gt;right)\r\n        return s;    \r\n    \/\/ case 1: s(l)\r\n    if (!t-&gt;right)\r\n      return s + \"(\" + tree2str(t-&gt;left) + \")\";\r\n    \/\/ general: s(l)(r)\r\n    return s + \"(\" + tree2str(t-&gt;left) + \")\" + \"(\" + tree2str(t-&gt;right) + \")\";\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47,45],"tags":[17,4,28],"class_list":["post-3665","post","type-post","status-publish","format-standard","hentry","category-string","category-tree","tag-recursion","tag-string","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3665"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3665\/revisions"}],"predecessor-version":[{"id":3668,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3665\/revisions\/3668"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}