{"id":742,"date":"2020-06-29T19:14:31","date_gmt":"2020-06-29T11:14:31","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=742"},"modified":"2020-06-29T19:14:31","modified_gmt":"2020-06-29T11:14:31","slug":"jz23-%e4%ba%8c%e5%8f%89%e6%90%9c%e7%b4%a2%e6%a0%91%e7%9a%84%e5%90%8e%e5%ba%8f%e9%81%8d%e5%8e%86%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=742","title":{"rendered":"JZ23 \u2014 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u540e\u5e8f\u904d\u5386\u5e8f\u5217"},"content":{"rendered":"<h3>\u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5224\u65ad\u8be5\u6570\u7ec4\u662f\u4e0d\u662f\u67d0\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u540e\u5e8f\u904d\u5386\u7684\u7ed3\u679c\u3002\u5982\u679c\u662f\u5219\u8f93\u51faYes,\u5426\u5219\u8f93\u51faNo\u3002\u5047\u8bbe\u8f93\u5165\u7684\u6570\u7ec4\u7684\u4efb\u610f\u4e24\u4e2a\u6570\u5b57\u90fd\u4e92\u4e0d\u76f8\u540c\u3002<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<p>\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u53c8\u53eb\u4e8c\u53c9\u6392\u5e8f\u6811\u3001\u4e8c\u53c9\u67e5\u627e\u6811\uff1a<code>\u7a7a\u6811<\/code>\uff0c\u6216<code>\u5de6\u5b69\u5b50\u5c0f\u4e8e\u7b49\u4e8e\u6839\u7ed3\u70b9<\/code>\u4e14<code>\u53f3\u5b69\u5b50\u5927\u4e8e\u7b49\u4e8e\u6839\u7ed3\u70b9<\/code>\u4e14<code>\u5de6\u5b50\u6811\u53f3\u5b50\u6811\u5747\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811<\/code>\u3002<\/p>\n<p>\u540e\u5e8f\u904d\u5386\u5e8f\u5217\uff0c\u6700\u540e\u4e00\u4e2a\u6570\u5b57\u4e3a\u6839<code>root<\/code>\uff0c\u5de6\u534a\u90e8\u5206\u5c0f\u4e8e<code>root<\/code>\u7684\u4e3a\u5de6\u5b50\u6811\uff0c\u53f3\u534a\u90e8\u5206\u5927\u4e8e<code>root<\/code>\u7684\u4e3a\u53f3\u5b50\u6811\u3002<br \/>\n\u8981\u6ce8\u610f\uff0c\u5de6\u53f3\u5b50\u6811\u90fd\u6709\u53ef\u80fd\u4e3a\u7a7a\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e00\u4e2a\u6570\u5b57\u548c<code>root<\/code>\u6bd4\u8f83\u5927\u5c0f\uff0c\u6765\u5224\u65ad\u662f\u5426\u5b58\u5728\u5de6\u5b50\u6811\u3002<\/p>\n<p>\u5224\u65ad<code>\u662f\u4e0d\u662f\u67d0\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u540e\u5e8f\u904d\u5386<\/code>\uff1a<br \/>\n0. \u5982\u679c\u5e8f\u5217\u5143\u7d20\u5c0f\u4e8e\u6216\u7b49\u4e8e1\uff0c\u8fd4\u56de\u771f\u3002<br \/>\n1. \u627e\u51fa\u6839\u7ed3\u70b9\uff0c\u5373\u6700\u540e\u4e00\u4e2a\u6570\u5b57\u3002<br \/>\n2. \u6839\u636e\u6570\u5b66\u5173\u7cfb\u627e\u51fa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff0c\u5982\u679c\u5e8f\u5217\u4e2d\u6709\u6570\u5b57\u5269\u4e0b\uff0c\u8bf4\u660e\u5f53\u524d\u5e8f\u5217\u4e0d\u80fd\u5206\u6210\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff0c\u8fd4\u56de\u5047\u3002<br \/>\n3. \u9012\u5f52\u8c03\u7528\u6b64\u8fc7\u7a0b\uff0c\u5982\u679c<code>\u5de6\u5b50\u6811\u662f\u67d0\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u540e\u5e8f\u904d\u5386<\/code>\u4e14<code>\u53f3\u5b50\u6811\u662f\u67d0\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u540e\u5e8f\u904d\u5386<\/code>\uff0c\u8fd4\u56de\u771f\uff0c\u5426\u5219\u8fd4\u56de\u5047\u3002<\/p>\n<h3>C++<\/h3>\n<pre><code class=\"language-cpp line-numbers\">class Solution\n{\npublic:\n    bool VerifySquenceOfBST(vector&lt;int&gt; sequence)\n    {\n        return sequence.size() &gt; 0 &amp;&amp; VerifySquenceOfBST2(sequence);\n    }\n    bool VerifySquenceOfBST2(vector&lt;int&gt; sequence)\n    {\n        if (sequence.size() &lt;= 1)\n            return true;\n        int root = sequence[sequence.size() - 1];\n        vector&lt;int&gt; left, right;\n        int i = 0;\n        if (sequence[0] &lt; root) \/\/\u5b58\u5728\u5de6\u5b50\u6811\n        {\n            for (; i &lt; sequence.size() - 1 &amp;&amp; sequence[i] &lt; root; i++)\n                left.push_back(sequence[i]);\n        }\n        for (; i &lt; sequence.size() - 1 &amp;&amp; sequence[i] &gt; root; i++)\n            right.push_back(sequence[i]);\n        if (i != sequence.size() - 1) \/\/\u53f3\u5b50\u6811\u4e0d\u89c4\u8303\n            return false;\n        return VerifySquenceOfBST2(left) &amp;&amp; VerifySquenceOfBST2(right);\n    }\n};\n<\/code><\/pre>\n<h3>Java<\/h3>\n<pre><code class=\"language-java line-numbers\">import java.util.*;\npublic class Solution {\n    public boolean VerifySquenceOfBST(int[] sequence) {\n        return sequence != null &amp;&amp; sequence.length &gt; 0 &amp;&amp; VerifySquenceOfBST2(sequence);\n    }\n\n    private boolean VerifySquenceOfBST2(int[] sequence) {\n        if (sequence.length &lt;= 1)\n            return true;\n        int root = sequence[sequence.length - 1];\n        int index = 0, mid;\n        if (sequence[0] &lt; root)\/\/ \u6709\u5de6\u5b50\u6811\n        {\n            while (index &lt; sequence.length - 1 &amp;&amp; sequence[index] &lt; root) {\n                index++;\n            }\n        }\n        mid = index;\n        while (index &lt; sequence.length - 1 &amp;&amp; sequence[index] &gt; root) {\n            index++;\n        }\n        if (index != sequence.length - 1)\/\/ \u53f3\u5b50\u6811\u53d1\u751f\u9519\u8bef\n            return false;\n        return VerifySquenceOfBST2(Arrays.copyOf(sequence, mid))\n                &amp;&amp; VerifySquenceOfBST2(Arrays.copyOfRange(sequence, mid, sequence.length - 1));\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u5224\u65ad\u8be5\u6570\u7ec4\u662f\u4e0d\u662f\u67d0\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u540e\u5e8f\u904d\u5386\u7684\u7ed3\u679c\u3002\u5982\u679c\u662f\u5219\u8f93\u51faYes,\u5426\u5219\u8f93\u51faNo\u3002\u5047 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[33,39],"class_list":["post-742","post","type-post","status-publish","format-standard","hentry","category-algo","tag-33","tag-offer"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=742"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/742\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}