{"id":603,"date":"2020-09-26T11:02:16","date_gmt":"2020-09-26T03:02:16","guid":{"rendered":"http:\/\/39.96.58.60\/?p=603"},"modified":"2022-10-18T16:45:05","modified_gmt":"2022-10-18T08:45:05","slug":"leetcode-117-%e5%a1%ab%e5%85%85%e6%af%8f%e4%b8%aa%e8%8a%82%e7%82%b9%e7%9a%84%e4%b8%8b%e4%b8%80%e4%b8%aa%e5%8f%b3%e4%be%a7%e8%8a%82%e7%82%b9%e6%8c%87%e9%92%88-ii","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=603","title":{"rendered":"Leetcode 117. \u586b\u5145\u6bcf\u4e2a\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u6307\u9488 II"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811<\/p>\n<pre>struct Node {\n\n  int val;\n\n  Node *left;\n\n  Node *right;\n\n  Node *next;\n\n}<\/pre>\n<p>\u586b\u5145\u5b83\u7684\u6bcf\u4e2a next \u6307\u9488\uff0c\u8ba9\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u5176\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u3002\u5982\u679c\u627e\u4e0d\u5230\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\uff0c\u5219\u5c06 next \u6307\u9488\u8bbe\u7f6e\u4e3a <code>NULL<\/code>\u3002<\/p>\n<p>\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709&nbsp;next \u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a <code>NULL<\/code>\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8fdb\u9636\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f60\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u7ea7\u989d\u5916\u7a7a\u95f4\u3002<\/li>\n<li>\u4f7f\u7528\u9012\u5f52\u89e3\u9898\u4e5f\u7b26\u5408\u8981\u6c42\uff0c\u672c\u9898\u4e2d\u9012\u5f52\u7a0b\u5e8f\u5360\u7528\u7684\u6808\u7a7a\u95f4\u4e0d\u7b97\u505a\u989d\u5916\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2019\/02\/15\/117_sample.png\" style=\"height: 218px; width: 640px;\"><\/p>\n<pre><strong>\u8f93\u5165<\/strong>\uff1aroot = [1,2,3,4,5,null,7]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,#,2,3,#,4,5,7,#]\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u7ed9\u5b9a\u4e8c\u53c9\u6811\u5982\u56fe A \u6240\u793a\uff0c\u4f60\u7684\u51fd\u6570\u5e94\u8be5\u586b\u5145\u5b83\u7684\u6bcf\u4e2a next \u6307\u9488\uff0c\u4ee5\u6307\u5411\u5176\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\uff0c\u5982\u56fe B \u6240\u793a\u3002<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u6811\u4e2d\u7684\u8282\u70b9\u6570\u5c0f\u4e8e <code>6000<\/code><\/li>\n<li><code>-100&nbsp;&lt;= node.val &lt;= 100<\/code><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ul><\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6811\u3001 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\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\uff1a60 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8681.56% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8654.12% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5148\u628a\u6bcf\u5c42\u7684\u8282\u70b9\u4ee5\u81ea\u5de6\u5411\u53f3\u7684\u987a\u5e8f\u4fdd\u5b58\uff0c\n    \u7136\u540e\u4fee\u6539next\n\"\"\"\nclass Solution:\n    def connect(self, root: 'Node') -&gt; 'Node':\n        record = {}\n        def find(root, h):  # \u904d\u5386\uff0c\u5e76\u4fdd\u5b58\u6bcf\u5c42\u7684\u8282\u70b9\n            if root:\n                if h in record:\n                    record[h].append(root)\n                else:\n                    record[h] = [root]\n                find(root.left, h+1)    # \u81ea\u5de6\u5411\u53f3\u7684\u987a\u5e8f\u904d\u5386\n                find(root.right, h+1)\n\n        find(root, 1)\n        for h, ns in record.items():    # \u5904\u7406\u6bcf\u5c42\u7684\u8282\u70b9\n            for i in range(len(ns)-1):\n                ns[i].next = ns[i+1]\n            ns[-1] = None\n        return root<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811 struct Node { int val; Node *left; Node *right;&#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":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/603"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=603"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/603\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}