{"id":629,"date":"2017-10-19T00:02:58","date_gmt":"2017-10-19T07:02:58","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=629"},"modified":"2018-04-19T08:35:55","modified_gmt":"2018-04-19T15:35:55","slug":"leetcode-449-serialize-and-deserialize-bst","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-449-serialize-and-deserialize-bst\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 449. Serialize and Deserialize BST"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 449. Serialize and Deserialize BST - \u5237\u9898\u627e\u5de5\u4f5c EP91\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/GDqVCQcmxgU?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<p><strong>Problem:<\/strong><\/p>\n<p>Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.<\/p>\n<p>Design an algorithm to serialize and deserialize a\u00a0<b>binary search tree<\/b>. There is no restriction on how your serialization\/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.<\/p>\n<p><b>The encoded string should be as compact as possible.<\/b><\/p>\n<p><b>Note:<\/b>\u00a0Do not use class member\/global\/static variables to store states. Your serialize and deserialize algorithms should be stateless.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/449-ep91.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-640\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/449-ep91.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/449-ep91.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/449-ep91-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/449-ep91-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/449-ep91-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Binary format<\/p>\n<p>serialized size: 4*n bytes, n is the number of nodes in the BST.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 19 ~ 26 ms (&lt;93.31%)\r\nclass Codec {\r\npublic:\r\n\r\n    \/\/ Encodes a tree to a single string.\r\n    string serialize(TreeNode* root) {\r\n        string s;\r\n        serialize(root, s);\r\n        return s;\r\n    }\r\n\r\n    \/\/ Decodes your encoded data to tree.\r\n    TreeNode* deserialize(string data) {        \r\n        int pos = 0;\r\n        return deserialize(data, pos, INT_MIN, INT_MAX);\r\n    }\r\nprivate:\r\n    void serialize(TreeNode* root, string&amp; s) {\r\n        if (!root) return;    \r\n        s.append(reinterpret_cast&lt;const char*&gt;(&amp;root-&gt;val), sizeof(root-&gt;val));\r\n        serialize(root-&gt;left, s);\r\n        serialize(root-&gt;right, s);\r\n    }\r\n    \r\n    TreeNode* deserialize(const string&amp; s, int&amp; pos, int curMin, int curMax) {\r\n        if (pos &gt;= s.size()) return nullptr;\r\n        int val = *reinterpret_cast&lt;const int*&gt;(s.data() + pos);\r\n        if (val &lt; curMin || val &gt; curMax) return nullptr;\r\n        pos += sizeof(val);\r\n        TreeNode* root = new TreeNode(val);\r\n        root-&gt;left = deserialize(s, pos, curMin, val);\r\n        root-&gt;right = deserialize(s, pos, val, curMax);\r\n        return root;\r\n    }\r\n};\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Related Problems<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-297-serialize-and-deserialize-binary-tree\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 297. Serialize and Deserialize Binary Tree<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[164,45],"tags":[133,134,94],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-medium","category-tree","tag-binary-fortmat","tag-deserializtion","tag-serialization","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/629","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=629"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":2628,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/629\/revisions\/2628"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}