{"id":2474,"date":"2022-01-29T13:53:44","date_gmt":"2022-01-29T05:53:44","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2474"},"modified":"2022-01-29T19:39:13","modified_gmt":"2022-01-29T11:39:13","slug":"leetcodeday102","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/29\/leetcodeday102\/","title":{"rendered":"leetcodeday102&#8211;\u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\uff08!\uff09"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;<code>root<\/code>&nbsp;\uff0c\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684&nbsp;<strong>\u5c42\u5e8f\u904d\u5386<\/strong>&nbsp;\u3002 \uff08\u5373\u9010\u5c42\u5730\uff0c\u4ece\u5de6\u5230\u53f3\u8bbf\u95ee\u6240\u6709\u8282\u70b9\uff09\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/19\/tree1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>root = &#91;3,9,20,null,null,15,7]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;3],&#91;9,20],&#91;15,7]]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>root = &#91;1]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;1]]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>root = &#91;]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;]<\/code><\/pre>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u5c42\u5e8f\u904d\u5386\u601d\u8def\uff08\u961f\u5217\uff09\uff1a \u4e8c\u53c9\u6811\u7684\u8282\u70b9\u52a0\u5165\u961f\u5217\uff0c \u51fa\u961f\u7684\u540c\u65f6\u5c06\u5176\u975e\u7a7a\u5de6\u53f3\u5b69\u5b50\u4f9d\u6b21\u5165\u961f\uff0c\u51fa\u961f\u5230\u961f\u5217\u4e3a\u7a7a\u5373\u5b8c\u6210\u904d\u5386\u3002 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\n# @lc app=leetcode.cn id=102 lang=python3\n#\n# &#91;102] \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\n#\u601d\u8def\u5c31\u662f\u5c06\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u52a0\u5165\u961f\u5217\uff0c\n# \u51fa\u961f\u7684\u540c\u65f6\u5c06\u5176\u975e\u7a7a\u5de6\u53f3\u5b69\u5b50\u4f9d\u6b21\u5165\u961f\uff0c\u51fa\u961f\u5230\u961f\u5217\u4e3a\u7a7a\u5373\u5b8c\u6210\u904d\u5386\u3002\n\n# @lc code=start\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def levelOrder(self, root: TreeNode) -&gt; List&#91;List&#91;int]]:\n        \"\"\"\n        :type root: TreeNode\n        :rtype: List&#91;List&#91;int]]\n        \"\"\"\n        if not root: return &#91;] #\u6ce8\u610f\u7279\u6b8a\u60c5\u51b5\uff1a\u6811\u4e3a\u7a7a\u8fd4\u56de&#91;]\n        queue = &#91;root]\n        list1 = &#91;]\n        while queue:\n            list2 = &#91;]\n            for i in range(len(queue)):\n                a = queue.pop(0)#\u5143\u7d20\u51fa\u961f\u5217\n                if a.left:\n                    queue.append(a.left)\n                if a.right:\n                    queue.append(a.right)\n                list2.append(a.val)\n            list1.append(list2)\n        return list1\n            \n# @lc code=end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"842\" height=\"225\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-308.png\" alt=\"\" class=\"wp-image-2475\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-308.png 842w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-308-300x80.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-308-768x205.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9&nbsp;root&nbsp;\uff0c\u8fd4\u56de\u5176\u8282\u70b9\u503c\u7684&nbsp;\u5c42\u5e8f\u904d\u5386&nbsp;\u3002 \uff08\u5373\u9010\u5c42 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/29\/leetcodeday102\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday102&#8211;\u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\uff08!\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2474"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2474"}],"version-history":[{"count":4,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2474\/revisions"}],"predecessor-version":[{"id":2493,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2474\/revisions\/2493"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2474"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}