{"id":123,"date":"2024-04-26T13:13:44","date_gmt":"2024-04-26T05:13:44","guid":{"rendered":"https:\/\/seanxd.com\/?p=123"},"modified":"2024-04-26T13:13:45","modified_gmt":"2024-04-26T05:13:45","slug":"zerojudge-d526","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-d526\/","title":{"rendered":"ZeroJudge D526: Binary Search Tree (BST)"},"content":{"rendered":"\n\n\n<p class=\"\">\u5c07\u4e0b\u5217\u5efa\u503c\u8f38\u5165\uff0c\u76f4\u63a5\u5efa\u7acb\u4e00\u500b\u4e8c\u5143\u641c\u5c0b\u6a39\uff0c 368\u3001115\u3001121\u300188\u3001741\u3001762\u3001801\u300134\u300141\u3001511\u300160\u3002\u6b32\u627e\u5efa\u503c\u70ba34\u7684\u7bc0\u9ede\uff0c\u5f9e368\u7bc0\u9ede\u70ba\u7b2c\u4e00\u6b21\u8d77\u7b97\uff0c\u9700\u8981\u505a\u5e7e\u6b21\u6bd4\u8f03 ?&nbsp;<\/p>\n\n\n\n<p class=\"\">\u53ea\u662f\u60f3\u8acb\u4f60<strong>\u5efa\u51fa\u4e00\u500b\u4e8c\u5143\u641c\u5c0b\u6a39\uff0c\u4e26\u8f38\u51fa\u6b64\u6a39\u7684\u524d\u5e8f\u641c\u5c0b<\/strong> (\u4e2d\u3001\u5de6\u3001\u53f3)\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"is-style-regular wp-block-table nfd-wb-animate nfd-wb-fade-in-bottom-short\" style=\"margin-top:0;margin-right:0;margin-bottom:0;margin-left:0;font-size:clamp(14px, 0.875rem + ((1vw - 3.2px) * 0.234), 17px);\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td><strong>EOF<\/strong> \u8f38\u5165\uff0c\u7b2c\u4e00\u884c\u6709\u4e00\u500b\u6578\u5b57 <strong>N<\/strong> (1 \u2266 <strong>N<\/strong> \u2266 1000)\u3002<br>\u7b2c\u4e8c\u884c\u6703\u5efa\u5165 <strong>N<\/strong> \u500b\u6578\u5b57 M (1 \u2266 M \u2266231-1) \uff0c\u4e14\u6c92\u6709\u6578\u5b57\u6703\u91cd\u8907\u3002<\/td><td>\u8f38\u51fa\u8a72\u6a39\u7684<strong>\u524d\u5e8f\u641c\u5c0b\u7d50\u679c<\/strong>\u3002<\/td><\/tr><tr><td><strong>11<\/strong><br>368  115  121  88  741  762  801  34  41  511  60<br><strong>6<\/strong><br>5  2  10  4  9  15<\/td><td>368  115  88  34  41  60  121  741  511  762  801<br>5  2  4  10  9  15<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p class=\"\">\u5efa\u7acb\u4e00\u500b<strong>Struct<\/strong>\uff0c\u88e1\u9762\u5b58<strong>\u76ee\u524d\u7684\u503c\u548c\u5de6\u53f3\u5169\u908a\u7684\u8cc7\u6599<\/strong> (Value\u3001Left\u3001Right)\u3002\u5c07\u8cc7\u6599\u8f38\u5165\u4e4b\u5f8c\u4f7f\u7528<strong>For<\/strong>\u8ff4\u5708\u5c07\u8cc7\u6599\u4e00\u500b\u4e00\u500b\u9032\u884c\u5224\u65b7\u662f\u5426\u662f\u8981\u585e\u5230\u8cc7\u6599\u7d50\u69cb\u7684\u5de6\u908a\u9084\u662f\u53f3\u908a\uff0c\u5982\u679c\u8a72\u65b9\u5411\u5df2\u7d93\u6709\u8cc7\u6599\u4e86\u5c31\u8981\u5224\u65b7\u662f\u8981\u5728\u9019\u500b\u8cc7\u6599\u5f80\u53f3\u9084\u662f\u5f80\u5de6\u653e\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>while(true)<\/strong>\u4f86\u9032\u884c\u5224\u65b7\uff0c\u5c07\u5df2\u7d93\u5b58\u5728\u7684\u8cc7\u6599\u5f80\u5de6\/\u53f3\u908a\u63a8\u4e00\u500b\uff0c\u56e0\u70ba<strong>Struct<\/strong>\u672c\u8eab\u88e1\u9762\u7684\u5de6\u53f3\u5169\u908a\u8cc7\u6599\u4e5f\u662f<strong>Struct<\/strong>\uff0c\u6240\u4ee5\u6703\u5f62\u6210\u4e00\u500b\u7121\u9650\u7684\u8ff4\u5708\u72c0\u614b\u53ef\u4ee5\u4e00\u76f4\u585e\u8cc7\u6599\u3002\u5c07\u4e8c\u5143\u641c\u5c0b\u6a39\u5efa\u7acb\u5b8c\u4e4b\u5f8c\u53ef\u4ee5\u4f7f\u7528\u905e\u8ff4\u5c07\u6700\u4e00\u958b\u59cb\u7684<strong>Struct<\/strong>\u4e2d\u7684\u8cc7\u6599\u8f38\u51fa\uff0c\u5982\u679c\u6709\u5de6\u908a\u7684\u8a71\u5c31\u4e00\u76f4\u5f80\u5de6\u908a\u8f38\u51fa\uff0c\u7576\u5de6\u908a\u6c92\u6709\u8cc7\u6599\u4e4b\u5f8c\u518d\u5f9e\u6700\u4e0b\u9762\u5f80\u56de\u627e\u6709\u6c92\u6709\u8981\u8f38\u51fa\u7684\u53f3\u908a\uff0c\u4ee5\u6b64\u985e\u63a8\u5230\u6a39\u72c0\u5716\u6700\u53f3\u4e0b\u89d2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=d526\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge D526: Binary Search Tree (BST)<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap nfd-wb-animate nfd-wb-reveal-right\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nstruct Node {\n    int value;\n    struct Node *left;\n    struct Node *right;\n};\n\nvoid again(Node* hi)\n{\n    if (hi-&gt;left != nullptr)\n    {\n        cout &lt;&lt; hi-&gt;left-&gt;value &lt;&lt; &quot; &quot;;\n        again(hi-&gt;left);\n    }\n    if (hi-&gt;right != nullptr)\n    {\n        cout &lt;&lt; hi-&gt;right-&gt;value &lt;&lt; &quot; &quot;;\n        again(hi-&gt;right);\n    }\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(nullptr);\n    int N;\n    while (cin &gt;&gt; N)\n    {\n        vector&lt;int&gt;a;\n        for (int i = 0; i&lt;N; i++)\n        {\n            int tmp;\n            cin &gt;&gt; tmp;\n            a.push_back(tmp);\n        }\n        Node* sean = new Node();\n        sean-&gt;value = a[0];\n        Node* root;\n        for(int i = 1; i &lt; N; i++) {\n            root = sean;\n            Node* p = new Node();\n            p-&gt;value = a[i];\n            while(true) {\n                if(root-&gt;value &lt; a[i]) {\n                    if(root-&gt;right == nullptr) {\n                        root-&gt;right = p;\n                        break;\n                    } else {\n                        root = root-&gt;right;\n                    }\n                }\n                else if(root-&gt;value &gt; a[i]) {\n                    if(root-&gt;left == nullptr) {\n                        root-&gt;left = p;\n                        break;\n                    } else {\n                        root = root-&gt;left;\n                    }\n                }\n            }\n        }\n        cout &lt;&lt; sean-&gt;value &lt;&lt; &quot; &quot;;\n        again(sean);\n        cout &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge D526\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5c07\u4e0b\u5217\u5efa\u503c\u8f38\u5165\uff0c\u76f4\u63a5\u5efa\u7acb\u4e00\u500b\u4e8c\u5143\u641c\u5c0b\u6a39\uff0c 368\u3001115\u3001121\u300188\u3001741\u3001762\u3001801\u300134\u300141\u30015 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[6],"tags":[20,26,8,9,28],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-6","tag-20","tag-26","tag-8","tag-9","tag-28"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":125,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/123\/revisions\/125"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}