From ed4543b30042ab127a261b660054e54aaaf3102f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Thu, 1 Jun 2023 04:03:00 -0500 Subject: [PATCH] =?UTF-8?q?Update=200968.=E7=9B=91=E6=8E=A7=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0968.监控二叉树.md | 114 ++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 33 deletions(-) diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index fdec56ca..e2ba8ebf 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -371,56 +371,104 @@ class Solution { ### Python - +贪心(版本一) ```python class Solution: - def minCameraCover(self, root: TreeNode) -> int: - # Greedy Algo: + # Greedy Algo: # 从下往上安装摄像头:跳过leaves这样安装数量最少,局部最优 -> 全局最优 # 先给leaves的父节点安装,然后每隔两层节点安装一个摄像头,直到Head # 0: 该节点未覆盖 # 1: 该节点有摄像头 # 2: 该节点有覆盖 + def minCameraCover(self, root: TreeNode) -> int: + # 定义递归函数 + result = [0] # 用于记录摄像头的安装数量 + if self.traversal(root, result) == 0: + result[0] += 1 - result = 0 - # 从下往上遍历:后序(左右中) - def traversal(curr: TreeNode) -> int: - nonlocal result + return result[0] - if not curr: return 2 - left = traversal(curr.left) - right = traversal(curr.right) + + def traversal(self, cur: TreeNode, result: List[int]) -> int: + if not cur: + return 2 - # Case 1: - # 左右节点都有覆盖 - if left == 2 and right == 2: - return 0 + left = self.traversal(cur.left, result) + right = self.traversal(cur.right, result) - # Case 2: - # left == 0 && right == 0 左右节点无覆盖 - # left == 1 && right == 0 左节点有摄像头,右节点无覆盖 - # left == 0 && right == 1 左节点有无覆盖,右节点摄像头 - # left == 0 && right == 2 左节点无覆盖,右节点覆盖 - # left == 2 && right == 0 左节点覆盖,右节点无覆盖 - elif left == 0 or right == 0: - result += 1 - return 1 + # 情况1: 左右节点都有覆盖 + if left == 2 and right == 2: + return 0 - # Case 3: - # left == 1 && right == 2 左节点有摄像头,右节点有覆盖 - # left == 2 && right == 1 左节点有覆盖,右节点有摄像头 - # left == 1 && right == 1 左右节点都有摄像头 - elif left == 1 or right == 1: - return 2 + # 情况2: + # left == 0 && right == 0 左右节点无覆盖 + # left == 1 && right == 0 左节点有摄像头,右节点无覆盖 + # left == 0 && right == 1 左节点无覆盖,右节点有摄像头 + # left == 0 && right == 2 左节点无覆盖,右节点覆盖 + # left == 2 && right == 0 左节点覆盖,右节点无覆盖 + if left == 0 or right == 0: + result[0] += 1 + return 1 - # 其他情况前段代码均已覆盖 + # 情况3: + # left == 1 && right == 2 左节点有摄像头,右节点有覆盖 + # left == 2 && right == 1 左节点有覆盖,右节点有摄像头 + # left == 1 && right == 1 左右节点都有摄像头 + if left == 1 or right == 1: + return 2 - if traversal(root) == 0: - result += 1 - return result ``` +贪心(版本二)利用elif精简代码 +```python +class Solution: + # Greedy Algo: + # 从下往上安装摄像头:跳过leaves这样安装数量最少,局部最优 -> 全局最优 + # 先给leaves的父节点安装,然后每隔两层节点安装一个摄像头,直到Head + # 0: 该节点未覆盖 + # 1: 该节点有摄像头 + # 2: 该节点有覆盖 + def minCameraCover(self, root: TreeNode) -> int: + # 定义递归函数 + result = [0] # 用于记录摄像头的安装数量 + if self.traversal(root, result) == 0: + result[0] += 1 + return result[0] + + + def traversal(self, cur: TreeNode, result: List[int]) -> int: + if not cur: + return 2 + + left = self.traversal(cur.left, result) + right = self.traversal(cur.right, result) + + # 情况1: 左右节点都有覆盖 + if left == 2 and right == 2: + return 0 + + # 情况2: + # left == 0 && right == 0 左右节点无覆盖 + # left == 1 && right == 0 左节点有摄像头,右节点无覆盖 + # left == 0 && right == 1 左节点无覆盖,右节点有摄像头 + # left == 0 && right == 2 左节点无覆盖,右节点覆盖 + # left == 2 && right == 0 左节点覆盖,右节点无覆盖 + elif left == 0 or right == 0: + result[0] += 1 + return 1 + + # 情况3: + # left == 1 && right == 2 左节点有摄像头,右节点有覆盖 + # left == 2 && right == 1 左节点有覆盖,右节点有摄像头 + # left == 1 && right == 1 左右节点都有摄像头 + else: + return 2 + + + + +``` ### Go ```go