<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>binary fortmat Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/binary-fortmat/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/binary-fortmat/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:35:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>binary fortmat Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/binary-fortmat/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 449. Serialize and Deserialize BST</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-449-serialize-and-deserialize-bst/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-449-serialize-and-deserialize-bst/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 19 Oct 2017 07:02:58 +0000</pubDate>
				<category><![CDATA[Medium]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[binary fortmat]]></category>
		<category><![CDATA[deserializtion]]></category>
		<category><![CDATA[serialization]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=629</guid>

					<description><![CDATA[<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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-449-serialize-and-deserialize-bst/">花花酱 LeetCode 449. Serialize and Deserialize BST</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/GDqVCQcmxgU?feature=oembed" frameborder="0" gesture="media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<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>
<p>Design an algorithm to serialize and deserialize a <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>
<p><b>The encoded string should be as compact as possible.</b></p>
<p><b>Note:</b> Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.</p>
<p><strong>Idea:</strong></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/449-ep91.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></p>
<p>Binary format</p>
<p>serialized size: 4*n bytes, n is the number of nodes in the BST.</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 19 ~ 26 ms (&lt;93.31%)
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string s;
        serialize(root, s);
        return s;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {        
        int pos = 0;
        return deserialize(data, pos, INT_MIN, INT_MAX);
    }
private:
    void serialize(TreeNode* root, string&amp; s) {
        if (!root) return;    
        s.append(reinterpret_cast&lt;const char*&gt;(&amp;root-&gt;val), sizeof(root-&gt;val));
        serialize(root-&gt;left, s);
        serialize(root-&gt;right, s);
    }
    
    TreeNode* deserialize(const string&amp; s, int&amp; pos, int curMin, int curMax) {
        if (pos &gt;= s.size()) return nullptr;
        int val = *reinterpret_cast&lt;const int*&gt;(s.data() + pos);
        if (val &lt; curMin || val &gt; curMax) return nullptr;
        pos += sizeof(val);
        TreeNode* root = new TreeNode(val);
        root-&gt;left = deserialize(s, pos, curMin, val);
        root-&gt;right = deserialize(s, pos, val, curMax);
        return root;
    }
};</pre><p>&nbsp;</p>
<p><strong>Related Problems</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-297-serialize-and-deserialize-binary-tree/">[解题报告] LeetCode 297. Serialize and Deserialize Binary Tree</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-449-serialize-and-deserialize-bst/">花花酱 LeetCode 449. Serialize and Deserialize BST</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-449-serialize-and-deserialize-bst/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
