{"id":733,"date":"2020-09-26T11:15:46","date_gmt":"2020-09-26T03:15:46","guid":{"rendered":"http:\/\/39.96.58.60\/?p=733"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-331-%e9%aa%8c%e8%af%81%e4%ba%8c%e5%8f%89%e6%a0%91%e7%9a%84%e5%89%8d%e5%ba%8f%e5%ba%8f%e5%88%97%e5%8c%96","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=733","title":{"rendered":"Leetcode 331. \u9a8c\u8bc1\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u5e8f\u5217\u5316"},"content":{"rendered":"<p>\u5e8f\u5217\u5316\u4e8c\u53c9\u6811\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u524d\u5e8f\u904d\u5386\u3002\u5f53\u6211\u4eec\u9047\u5230\u4e00\u4e2a\u975e\u7a7a\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8bb0\u5f55\u4e0b\u8fd9\u4e2a\u8282\u70b9\u7684\u503c\u3002\u5982\u679c\u5b83\u662f\u4e00\u4e2a\u7a7a\u8282\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6807\u8bb0\u503c\u8bb0\u5f55\uff0c\u4f8b\u5982 <code>#<\/code>\u3002<\/p>\n<pre>     _9_\n\n    \/   \\\n\n   3     2\n\n  \/ \\   \/ \\\n\n 4   1  #  6\n\n\/ \\ \/ \\   \/ \\\n\n# # # #   # #\n\n<\/pre>\n<p>\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u4e8c\u53c9\u6811\u53ef\u4ee5\u88ab\u5e8f\u5217\u5316\u4e3a\u5b57\u7b26\u4e32 <code>\"9,3,4,#,#,1,#,#,2,#,6,#,#\"<\/code>\uff0c\u5176\u4e2d <code>#<\/code> \u4ee3\u8868\u4e00\u4e2a\u7a7a\u8282\u70b9\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e32\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5e8f\u5217\uff0c\u9a8c\u8bc1\u5b83\u662f\u5426\u662f\u6b63\u786e\u7684\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u5e8f\u5217\u5316\u3002\u7f16\u5199\u4e00\u4e2a\u5728\u4e0d\u91cd\u6784\u6811\u7684\u6761\u4ef6\u4e0b\u7684\u53ef\u884c\u7b97\u6cd5\u3002<\/p>\n<p>\u6bcf\u4e2a\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u6216\u4e3a\u4e00\u4e2a\u6574\u6570\u6216\u4e3a\u4e00\u4e2a\u8868\u793a <code>null<\/code> \u6307\u9488\u7684 <code>'#'<\/code> \u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u8f93\u5165\u683c\u5f0f\u603b\u662f\u6709\u6548\u7684\uff0c\u4f8b\u5982\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u5305\u542b\u4e24\u4e2a\u8fde\u7eed\u7684\u9017\u53f7\uff0c\u6bd4\u5982&nbsp;<code>\"1,,3\"<\/code> \u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165: <\/strong><code>\"9,3,4,#,#,1,#,#,2,#,6,#,#\"<\/code>\n\n<strong>\u8f93\u51fa: <\/strong><code>true<\/code><\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165: <\/strong><code>\"1,#\"<\/code>\n\n<strong>\u8f93\u51fa: <\/strong><code>false<\/code>\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n<pre><strong>\u8f93\u5165: <\/strong><code>\"9,#,#,1\"<\/code>\n\n<strong>\u8f93\u51fa: <\/strong><code>false<\/code><\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6808\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a48 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8642.51% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.11% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6808\n    \u6bcf\u6b21\u5220\u9664\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff0c\u5269\u4f59\u7684\u5e8f\u5217\u4ecd\u6784\u6210\u6811\n    \u6bcf\u5220\u9664\u4e00\u4e2a\u975eNone\u8282\u70b9\uff0c\u5747\u4f1a\u5220\u9664\u5176\u5b50\u6811\uff0c\u4e5f\u5c31\u662f\u4e24\u4e2aNone\u8282\u70b9\u3002\n    \u5220\u9664\u4e00\u4e2a\u975eNone\u8282\u70b9\u540e\uff0c\u4f1a\u4f7f\u5176\u6839\uff0c\u6dfb\u52a0\u4e00\u4e2aNone\u8282\u70b9\u3002\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def isValidSerialization(self, preorder: str) -&gt; bool:\n        record = []\n        for p in preorder.split(','):\n            record.append(p)\n\n        def pop_node(stack):    # \u5220\u9664\u5f53\u524d\u6811\u5e8f\u5217\u4e2d\u6700\u53f3\u4fa7\u7684\u975eNone\u8282\u70b9\n            if len(stack) &gt;= 3 and stack[-1] == '#' and stack[-2] == '#':   # \u975eNone\u8282\u70b9\u5fc5\u7136\u5b58\u5728\u4e24\u4e2aNone\u8282\u70b9;\u53ea\u5b58\u5728\u6839\u8282\u70b9\uff0c\u5176\u5e8f\u5217\u4e5f\u5fc5\u7136&gt;=3\n                stack.pop() # \u9996\u5148\u5220\u9664\u4e24\u4e2aNone\u8282\u70b9\n                stack.pop()\n                n = 1   # \u7edf\u8ba1\u540e\u7eed\u9700\u8981\u8865\u5165\u5e8f\u5217\u7684# None\u8282\u70b9\n                for p in stack[::-1]:   # \u5012\u53d9\u4ece\u5e8f\u5217\u4e2d\u5bfb\u627e\u975eNone\u8282\u70b9\uff0c\u5982\u679c\u662fNone\u8282\u70b9\uff0c\u5148\u51fa\u6808\uff0c\u5e76\u8ba1\u5165n\u4e2d\n                    if p == '#':\n                        n += 1\n                        stack.pop()\n                    else:   # \u627e\u5230\u975eNone\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n                        break\n                if stack:\n                    stack.pop() # \u5220\u9664\u975eNone\u8282\u70b9\n                    stack += ['#'] * n  # \u5c06n\u4e2aNone \u586b\u5165\u5e8f\u5217\u4e2d\n                    if pop_node(stack): # \u7ee7\u7eed\u9012\u5f52\u5220\u9664\u5f53\u524d\u6811\u4e2d\u7684\u6700\u53f3\u4fa7\u975eNone\u8282\u70b9\n                        return True\n                    else:\n                        return False\n                else:\n                    return False\n            elif stack == ['#']:    # \u5982\u679c\u5f53\u524d\u6811 len&lt;3\uff0c \u4e14\u4e3aNone\uff0c\u5219\u8bf4\u660e\u6240\u6709\u8282\u70b9\u5747\u53ef\u5220\u9664\uff0c\u539f\u5e8f\u5217\u6784\u6210\u6811\uff0c\u8fd4\u56deTrue\n                return True\n            else:\n                return False\n\n        return pop_node(record)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5e8f\u5217\u5316\u4e8c\u53c9\u6811\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u524d\u5e8f\u904d\u5386\u3002\u5f53\u6211\u4eec\u9047\u5230\u4e00\u4e2a\u975e\u7a7a\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8bb0\u5f55\u4e0b\u8fd9\u4e2a\u8282\u70b9\u7684\u503c\u3002\u5982\u679c\u5b83\u662f\u4e00\u4e2a\u7a7a\u8282\u70b9&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/733"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=733"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":734,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/733\/revisions\/734"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}