{"id":2009,"date":"2016-04-26T20:24:50","date_gmt":"2016-04-26T12:24:50","guid":{"rendered":"http:\/\/boweihe.me\/?p=2009"},"modified":"2016-04-26T20:24:50","modified_gmt":"2016-04-26T12:24:50","slug":"leetcode-331-verify-preorder-serialization-of-a-binary-tree","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2009","title":{"rendered":"LeetCode 331. Verify Preorder Serialization of a Binary Tree"},"content":{"rendered":"<p>\u5982\u5bb6\u7f51\u7edc\u5b9e\u5728\u592a\u5361\u4e86\uff0c\u4e0d\u8fc7\u8fd8\u597d\u6ca1\u6709\u4eba\u52ab@!$#@W&#8230;<br \/>\n\u7a0d\u5fae\u8bf4\u4e00\u4e0b\u601d\u8def\u628a\uff0c\u8fd9\u4e2a\u95ee\u9898\u5c31\u662f\u5224\u65ad\u67d0\u4e00\u5c42\u4e0a\u7684\u8282\u70b9\u5b83\u7684\u5b69\u5b50\u4f1a\u4e0d\u4f1a\u201c\u6ea2\u51fa\u201d\uff0c\u6216\u8005\u7b2c\u4e00\u5c42\u7684\u8282\u70b9\u6709\u6ca1\u6709\u5b8c\u5168\u95ed\u5408\u3002\u6211\u7528\u4e00\u4e2a\u6808\u6765\u4fdd\u5b58\u6bcf\u4e00\u5c42\u7684\u5b69\u5b50\u6570\u76ee\uff08\u5305\u62ecnull\uff09\uff1a<\/p>\n<ul>\n<li>\u5f53\u78b0\u5230\u6570\u5b57\u7684\u65f6\u5019\uff0c\u538b\u4e00\u5c42\u8ba1\u6570\u4e3a0\u7684\u6808\uff0c\u8868\u793a\u65b0\u5f00\u4e86\u4e00\u5c42\u5e76\u4e14\u8fd9\u4e00\u5c42\u7684\u5b69\u5b50\u6570\u4e3a0<\/li>\n<li>\u5f53\u78b0\u5230#\u53f7\u65f6\uff0c\u8bf4\u660e\u591a\u4e86\u4e00\u4e2a\u7a7a\u5b69\u5b50\uff0c\u5f53\u524d\u6808\u9876\u7684\u8ba1\u6570+1\uff0c\u5e76\u4e14\u5224\u65ad\u5f53\u524d\u6808\u9876\u7684\u8ba1\u6570\u662f\u5426&gt;=2\u4e86\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u90a3\u8bf4\u660e\u8fd9\u4e00\u5c42\u5df2\u7ecf\u6ee1\u4e86\uff0c\u90a3\u5c31\u9000\u6808\uff0c\u7136\u540e\u518d\u7ee7\u7eed\u7ed9\u65b0\u6808\u9876\u8ba1\u6570+1\uff0c\u7ee7\u7eed\u5224\u65ad\u662f\u5426\u6ee1&#8230;.\u5982\u6b64\u5f80\u590d<\/li>\n<\/ul>\n<p>\u7531\u4e8e\u6709\u4e86#\u8868\u793a\u7a7a\u8282\u70b9\u6240\u4ee5\u5c42\u7ea7\u53d8\u5f97\u660e\u786e\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u4e00\u4e2a\u53f6\u8282\u70b9\u8bfb\u5165\u4e24\u4e2a#\uff0c\u5f53\u524d\u5c42\u7684\u8ba1\u6570\u7b49\u4e8e2\u540e\u5c31\u9000\u6808\uff0c\u5e76\u4e14\u7ed9\u7236\u4eb2\u5c42\u7684\u8ba1\u6570\u5668+1\uff08\u8868\u793a\u4e0b\u4e00\u5c42\u5df2\u7ecf\u7ed3\u675f\u4e86\uff0c\u7236\u4eb2\u591a\u4e86\u4e00\u4e2a\u5de6\/\u53f3\u5b69\u5b50\uff09\u3002<br \/>\n\u4ee3\u7801\uff08\u597d\u4e45\u6ca1\u6709\u7eaf\u624b\u6253\u4e00\u904dAC\u4e86\uff09<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    bool isValidSerialization(string preorder) {\n        int size = preorder.size();\n        stack&lt;int&gt; working;\n        int i = 0;\n        while(i &lt; size){\n            int end = preorder.find(',', i);\n            if(end == string::npos)\n                end = size;\n            if(preorder[i] != '#'){\n                \/\/number\n                working.push(0);\n            } else {\n                \/\/'#'\n                if(i == 0){\n                    \/\/The null root?!\n                    if(size &gt; 1) return false;\n                    else return true;\n                }\n                working.top() += 1;\n                while(working.size()&gt;1 &amp;&amp; working.top() &gt;= 2){\n                    working.pop();\n                    working.top() += 1;\n                }\n            }\n            if(working.size() == 1 &amp;&amp; working.top() &gt; 2)\n                return false;\n            i = end+1;\n        }\n        return (working.size()==1) &amp;&amp; (working.top()==2);\n    }\n};<\/pre>\n<p>BTW,\u82cf\u5dde\u7684\u4ea4\u901a\u72b6\u51b5\u548c\u8def\u9762\u6bd4\u676d\u5dde\u597d\u591a\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u5bb6\u7f51\u7edc\u5b9e\u5728\u592a\u5361\u4e86\uff0c\u4e0d\u8fc7\u8fd8\u597d\u6ca1\u6709\u4eba\u52ab@!$#@W&#8230; \u7a0d\u5fae\u8bf4\u4e00\u4e0b\u601d\u8def\u628a\uff0c\u8fd9\u4e2a\u95ee\u9898\u5c31\u662f\u5224\u65ad\u67d0\u4e00\u5c42\u4e0a\u7684\u8282\u70b9\u5b83\u7684\u5b69\u5b50\u4f1a\u4e0d\u4f1a\u201c\u6ea2\u51fa\u201d\uff0c\u6216\u8005\u7b2c\u4e00\u5c42\u7684\u8282\u70b9\u6709\u6ca1\u6709\u5b8c\u5168\u95ed\u5408\u3002\u6211\u7528\u4e00\u4e2a\u6808\u6765\u4fdd\u5b58\u6bcf\u4e00\u5c42\u7684\u5b69\u5b50\u6570\u76ee\uff08\u5305\u62ecnull\uff09\uff1a \u5f53\u78b0\u5230\u6570\u5b57\u7684\u65f6\u5019\uff0c\u538b\u4e00\u5c42\u8ba1\u6570\u4e3a0\u7684\u6808\uff0c\u8868\u793a\u65b0\u5f00\u4e86\u4e00\u5c42\u5e76\u4e14\u8fd9\u4e00\u5c42\u7684\u5b69\u5b50\u6570\u4e3a0 \u5f53\u78b0\u5230#\u53f7\u65f6\uff0c\u8bf4\u660e\u591a\u4e86\u4e00\u4e2a\u7a7a\u5b69\u5b50\uff0c\u5f53\u524d\u6808\u9876\u7684\u8ba1\u6570+1\uff0c\u5e76\u4e14\u5224\u65ad\u5f53\u524d\u6808\u9876\u7684\u8ba1\u6570\u662f\u5426&gt;=2\u4e86\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u90a3\u8bf4\u660e\u8fd9\u4e00\u5c42\u5df2\u7ecf\u6ee1\u4e86\uff0c\u90a3\u5c31\u9000\u6808\uff0c\u7136\u540e\u518d\u7ee7\u7eed\u7ed9\u65b0\u6808\u9876\u8ba1\u6570+1\uff0c\u7ee7\u7eed\u5224\u65ad\u662f\u5426\u6ee1&#8230;.\u5982\u6b64\u5f80\u590d \u7531\u4e8e\u6709\u4e86#\u8868\u793a\u7a7a\u8282\u70b9\u6240\u4ee5\u5c42\u7ea7\u53d8\u5f97\u660e\u786e\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u4e00\u4e2a\u53f6\u8282\u70b9\u8bfb\u5165\u4e24\u4e2a#\uff0c\u5f53\u524d\u5c42\u7684\u8ba1\u6570\u7b49\u4e8e2\u540e\u5c31\u9000\u6808\uff0c\u5e76\u4e14\u7ed9\u7236\u4eb2\u5c42\u7684\u8ba1\u6570\u5668+1\uff08\u8868\u793a\u4e0b\u4e00\u5c42\u5df2\u7ecf\u7ed3\u675f\u4e86\uff0c\u7236\u4eb2\u591a\u4e86\u4e00\u4e2a\u5de6\/\u53f3\u5b69\u5b50\uff09\u3002 \u4ee3\u7801\uff08\u597d\u4e45\u6ca1\u6709\u7eaf\u624b\u6253\u4e00\u904dAC\u4e86\uff09 class Solution { public: bool isValidSerialization(string preorder) { int size = preorder.size(); stack&lt;int&gt; working; int i = 0; while(i &lt; size){ int end = preorder.find(&#8216;,&#8217;, i); if(end == string::npos) end = size; if(preorder[i] != &#8216;#&#8217;){ \/\/number working.push(0); } else { \/\/&#8217;#&#8217; if(i == 0){ \/\/The null root?! if(size &gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,133],"class_list":["post-2009","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-133"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2009"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2009\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}