<?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>varify Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/varify/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/varify/</link>
	<description></description>
	<lastBuildDate>Sat, 10 Aug 2019 08:01:21 +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>varify Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/varify/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 331. Verify Preorder Serialization of a Binary Tree</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-331-verify-preorder-serialization-of-a-binary-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-331-verify-preorder-serialization-of-a-binary-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 13 Apr 2018 15:26:50 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[varify]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2480</guid>

					<description><![CDATA[<p>Problem 题目大意：验证二叉树前序遍历的序列化是否合法。 https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/description/ One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node&#8217;s&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-331-verify-preorder-serialization-of-a-binary-tree/">花花酱 LeetCode 331. Verify Preorder Serialization of a Binary Tree</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>题目大意：验证二叉树前序遍历的序列化是否合法。</p>
<p><a href="https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/description/">https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/description/</a></p>
<p>One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node&#8217;s value. If it is a null node, we record using a sentinel value such as <code>#</code>.</p>
<pre class="crayon:false">     _9_
    /   \
   3     2
  / \   / \
 4   1  #  6
/ \ / \   / \
# # # #   # #
</pre>
<p>For example, the above binary tree can be serialized to the string <code>"9,3,4,#,#,1,#,#,2,#,6,#,#"</code>, where <code>#</code>represents a null node.</p>
<p>Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.</p>
<p>Each comma separated value in the string must be either an integer or a character <code>'#'</code> representing <code>null</code>pointer.</p>
<p>You may assume that the input format is always valid, for example it could never contain two consecutive commas such as <code>"1,,3"</code>.</p>
<p><strong>Example 1:</strong><br />
<code>"9,3,4,#,#,1,#,#,2,#,6,#,#"</code><br />
Return <code>true</code></p>
<p><strong>Example 2:</strong><br />
<code>"1,#"</code><br />
Return <code>false</code></p>
<p><strong>Example 3:</strong><br />
<code>"9,#,#,1"</code><br />
Return <code>false</code></p>
<p><b>Credits:</b><br />
Special thanks to <a href="https://leetcode.com/discuss/user/dietpepsi">@dietpepsi</a> for adding this problem and creating all test cases.</p>
<h1><strong>Solution: Recursion</strong></h1>
<ol>
<li>If a node is not null, it must has two children, thus verify left subtree and right subtree recursively.</li>
<li>If a not is null, the current char must be &#8216;#&#8217;</li>
</ol>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(h)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 5 ms
class Solution {
public:
  bool isValidSerialization(string preorder) {
    int pos = 0;    
    return isValid(preorder, pos) &amp;&amp; pos == preorder.length();
  }
private:
  bool isValid(const string&amp; s, int&amp; pos) {    
    if (pos &gt;= s.length()) return false;
    if (isdigit(s[pos])) {      
      while (isdigit(s[pos])) ++pos;
      return isValid(s, ++pos) &amp;&amp; isValid(s, ++pos);
    }
    return s[pos++] == '#';
  }
};</pre><p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-331-verify-preorder-serialization-of-a-binary-tree/">花花酱 LeetCode 331. Verify Preorder Serialization of a Binary Tree</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-331-verify-preorder-serialization-of-a-binary-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
