{"id":549,"date":"2020-09-26T10:54:45","date_gmt":"2020-09-26T02:54:45","guid":{"rendered":"http:\/\/39.96.58.60\/?p=549"},"modified":"2022-10-18T16:38:54","modified_gmt":"2022-10-18T08:38:54","slug":"leetcode-111-%e4%ba%8c%e5%8f%89%e6%a0%91%e7%9a%84%e6%9c%80%e5%b0%8f%e6%b7%b1%e5%ba%a6","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=549","title":{"rendered":"Leetcode 111. \u4e8c\u53c9\u6811\u7684\u6700\u5c0f\u6df1\u5ea6"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u627e\u51fa\u5176\u6700\u5c0f\u6df1\u5ea6\u3002<\/p>\n<p>\u6700\u5c0f\u6df1\u5ea6\u662f\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fd1\u53f6\u5b50\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84\u4e0a\u7684\u8282\u70b9\u6570\u91cf\u3002<\/p>\n<p><strong>\u8bf4\u660e:<\/strong>&nbsp;\u53f6\u5b50\u8282\u70b9\u662f\u6307\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\u3002<\/p>\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e8c\u53c9\u6811&nbsp;<code>[3,9,20,null,null,15,7]<\/code>,<\/p>\n<pre>    3\n\n   \/ \\\n\n  9  20\n\n    \/  \\\n\n   15   7<\/pre>\n<p>\u8fd4\u56de\u5b83\u7684\u6700\u5c0f\u6df1\u5ea6 &nbsp;2.<\/p>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6811\u3001 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3001 \u5e7f\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\uff1a48 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8695.56% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.94% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5e7f\u5ea6\u4f18\u5148\u3002\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, x):\n#         self.val = x\n#         self.left = None\n#         self.right = None\n\nclass Solution:\n    def minDepth(self, root: TreeNode) -&gt; int:\n        if root == None:\n            return 0\n        roots = [root]  # \u7528\u4e8e\u4fdd\u5b58\u5f53\u524d\u9700\u8981 \u67e5\u770b\u7684\u8282\u70b9\uff0c \u4ee5\u6839\u4f5c\u4e3a\u8d77\u59cb\n        depth = 1       # \u7528\u4e8e\u4fdd\u5b58\u6811\u7684\u6700\u5c0f\u9ad8\u5ea6\n        while roots:\n            roots_c = []\n            for r in roots:\n                if r != None:   # \u5982\u679c\u5f53\u524d\u8282\u70b9 \u4e3aNone\uff0c\u8df3\u8fc7\uff0c\u67e5\u770b\u4e0b\u4e00\u4e2a\u8282\u70b9\n                    if r.left == None and r.right == None:  # \u5982\u679c\u5f53\u524d\u8282\u70b9 \u5de6\u5b50\u6811\u4e0e\u53f3\u5b50\u6811\u5747\u4e3aNone, \u5219\u5f53\u524d\u8282\u70b9\u4e3a\u53f6\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\u9ad8\u5ea6\u5373\u53ef\n                        return depth\n                    else:       # \u5de6\u53f3\u5b50\u6811\u5b58\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\uff0c\u4e0d\u662f\u53f6\u8282\u70b9\uff0c\u5219\u51c6\u5907\u67e5\u770b\u5176\u5de6\u53f3\u5b50\u6811\n                        roots_c.append(r.left)\n                        roots_c.append(r.right)\n            roots = roots_c\n            depth += 1  # \u904d\u5386\u5b8c\u4e00\u6b21\u540e\uff0c\u9ad8\u5ea6+1<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u627e\u51fa\u5176\u6700\u5c0f\u6df1\u5ea6\u3002 \u6700\u5c0f\u6df1\u5ea6\u662f\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fd1\u53f6\u5b50\u8282\u70b9\u7684\u6700\u77ed\u8def\u5f84\u4e0a\u7684\u8282\u70b9\u6570\u91cf\u3002 \u8bf4\u660e:&nbsp;&#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\/549"}],"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=549"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":550,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/549\/revisions\/550"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}