{"id":329,"date":"2020-09-26T10:26:34","date_gmt":"2020-09-26T02:26:34","guid":{"rendered":"http:\/\/39.96.58.60\/?p=329"},"modified":"2022-10-18T16:39:50","modified_gmt":"2022-10-18T08:39:50","slug":"leetcode-968-%e7%9b%91%e6%8e%a7%e4%ba%8c%e5%8f%89%e6%a0%91","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=329","title":{"rendered":"Leetcode 968. \u76d1\u63a7\u4e8c\u53c9\u6811"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5728\u6811\u7684\u8282\u70b9\u4e0a\u5b89\u88c5\u6444\u50cf\u5934\u3002<\/p>\n<p>\u8282\u70b9\u4e0a\u7684\u6bcf\u4e2a\u6444\u5f71\u5934\u90fd\u53ef\u4ee5\u76d1\u89c6<strong>\u5176\u7236\u5bf9\u8c61\u3001\u81ea\u8eab\u53ca\u5176\u76f4\u63a5\u5b50\u5bf9\u8c61\u3002<\/strong><\/p>\n<p>\u8ba1\u7b97\u76d1\u63a7\u6811\u7684\u6240\u6709\u8282\u70b9\u6240\u9700\u7684\u6700\u5c0f\u6444\u50cf\u5934\u6570\u91cf\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/12\/29\/bst_cameras_01.png\" style=\"height: 163px; width: 138px;\"><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[0,0,null,0,0]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>1\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5982\u56fe\u6240\u793a\uff0c\u4e00\u53f0\u6444\u50cf\u5934\u8db3\u4ee5\u76d1\u63a7\u6240\u6709\u8282\u70b9\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/12\/29\/bst_cameras_02.png\" style=\"height: 312px; width: 139px;\"><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[0,0,null,0,null,0,null,null,0]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>2\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u9700\u8981\u81f3\u5c11\u4e24\u4e2a\u6444\u50cf\u5934\u6765\u76d1\u89c6\u6811\u7684\u6240\u6709\u8282\u70b9\u3002 \u4e0a\u56fe\u663e\u793a\u4e86\u6444\u50cf\u5934\u653e\u7f6e\u7684\u6709\u6548\u4f4d\u7f6e\u4e4b\u4e00\u3002\n\n<\/pre>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li>\u7ed9\u5b9a\u6811\u7684\u8282\u70b9\u6570\u7684\u8303\u56f4\u662f&nbsp;<code>[1, 1000]<\/code>\u3002<\/li>\n<li>\u6bcf\u4e2a\u8282\u70b9\u7684\u503c\u90fd\u662f 0\u3002<\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Hard<\/p>\n<p>**\u6807\u7b7e**: \u6811\u3001 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3001 \u52a8\u6001\u89c4\u5212\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\u4e8664.62% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e866.61% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u540e\u5e8f\u904d\u5386\uff0c\u5148\u5904\u7406\u5de6\u53f3\u5b50\u6811\u3002\u5c06\u5b50\u6811\u4e3a\u7a7a\u770b\u505a\u5b50\u6811\u5df2\u8986\u76d6\u3002\n    \u5f53\u524d\u8282\u70b9\u5b58\u5728\u4ee5\u4e0b\u72b6\u6001\uff1a\n        1. \u53f6\u8282\u70b9\uff0c\u5de6\u53f3\u5b50\u6811\u4e3a\u7a7a\u3002\u770b\u505a\u5f53\u524d\u8282\u70b9\u5de6\u53f3\u5b50\u6811\u5747\u5df2\u8986\u76d6\uff0c\u5219\u5f53\u524d\u8282\u70b9\u5ffd\u7565\n        2. \u5de6\u53f3\u5b50\u6811\u5b58\u5728\u5b50\u6811\u4e0d\u8986\u76d6\uff0c\u5219\u5f53\u524d\u8282\u70b9\u653e\u7f6e\u76d1\u63a7\n        3. \u5982\u679c\u5de6\u53f3\u5b50\u6811\u653e\u7f6e\u76d1\u63a7\uff0c\u5219\u5f53\u524d\u8282\u70b9\u5df2\u8986\u76d6\n        4. \u5982\u679c\u5f53\u524d\u8282\u70b9\u662f\u6839\u8282\u70b9\uff0c\u4e14\u5de6\u53f3\u5b50\u6811\u5df2\u8986\u76d6\uff0c\u9700\u5728\u5f53\u524d\u8282\u70b9\u653e\u7f6e\u76d1\u63a7\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 minCameraCover(self, root: TreeNode) -&gt; int:\n        self.result = 0\n\n        def find(now_root):\n            if now_root.left:\n                find(now_root.left)\n            if now_root.right:\n                find(now_root.right)\n\n            l_val, r_val = 1, 1\n            if now_root.left:   # \u5de6\u5b50\u6811\u4e0d\u4e3a\u7a7a\uff0c\u5904\u7406\u5de6\u5b50\u6811\n                l_val = now_root.left.val\n            if now_root.right:  # \u53f3\u5b50\u6811\u4e0d\u4e3a\u7a7a\uff0c\u5904\u7406\u53f3\u5b50\u6811\n                r_val = now_root.right.val\n\n            if not (r_val and l_val):   # \u5982\u679c\u5de6\u53f3\u5b50\u6811\u5b58\u5728\u672a\u8986\u76d6\u8282\u70b9\uff0c\u5219\u5f53\u524d\u8282\u70b9 \u653e\u7f6e\u76d1\u63a7\n                now_root.val = 2\n                self.result += 1\n            elif now_root == root and l_val != 2 and r_val != 2:    # \u5982\u679c\u5b50\u6811\u5df2\u8986\u76d6\uff0c\u4f46\u5f53\u524d\u8282\u70b9\u4e3a\u6839\uff0c\u5219\u9700\u5355\u72ec\u653e\u7f6e\u4e00\u4e2a\u76d1\u63a7\n                print(now_root)\n                self.result += 1\n\n            elif r_val == 2 or l_val == 2:    # \u5982\u679c\u5de6\u53f3\u5b50\u6811\u4e2d\u653e\u7f6e\u4e86\u76d1\u63a7\uff0c\u5219\u5f53\u524d\u8282\u70b9\u88ab\u8986\u76d6\n                now_root.val = 1\n\n        if root.left or root.right:\n            find(root)\n            return self.result\n        else:\n            return 1\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5728\u6811\u7684\u8282\u70b9\u4e0a\u5b89\u88c5\u6444\u50cf\u5934\u3002 \u8282\u70b9\u4e0a\u7684\u6bcf\u4e2a\u6444\u5f71\u5934\u90fd\u53ef\u4ee5\u76d1\u89c6\u5176\u7236\u5bf9\u8c61\u3001\u81ea\u8eab\u53ca\u5176\u76f4\u63a5\u5b50\u5bf9\u8c61\u3002 \u8ba1\u7b97&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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\/329"}],"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=329"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":330,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/329\/revisions\/330"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}