mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0968.监控二叉树.md
This commit is contained in:
@ -371,56 +371,104 @@ class Solution {
|
|||||||
|
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
贪心(版本一)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def minCameraCover(self, root: TreeNode) -> int:
|
# Greedy Algo:
|
||||||
# Greedy Algo:
|
|
||||||
# 从下往上安装摄像头:跳过leaves这样安装数量最少,局部最优 -> 全局最优
|
# 从下往上安装摄像头:跳过leaves这样安装数量最少,局部最优 -> 全局最优
|
||||||
# 先给leaves的父节点安装,然后每隔两层节点安装一个摄像头,直到Head
|
# 先给leaves的父节点安装,然后每隔两层节点安装一个摄像头,直到Head
|
||||||
# 0: 该节点未覆盖
|
# 0: 该节点未覆盖
|
||||||
# 1: 该节点有摄像头
|
# 1: 该节点有摄像头
|
||||||
# 2: 该节点有覆盖
|
# 2: 该节点有覆盖
|
||||||
|
def minCameraCover(self, root: TreeNode) -> int:
|
||||||
|
# 定义递归函数
|
||||||
|
result = [0] # 用于记录摄像头的安装数量
|
||||||
|
if self.traversal(root, result) == 0:
|
||||||
|
result[0] += 1
|
||||||
|
|
||||||
result = 0
|
return result[0]
|
||||||
# 从下往上遍历:后序(左右中)
|
|
||||||
def traversal(curr: TreeNode) -> int:
|
|
||||||
nonlocal result
|
|
||||||
|
|
||||||
if not curr: return 2
|
|
||||||
left = traversal(curr.left)
|
def traversal(self, cur: TreeNode, result: List[int]) -> int:
|
||||||
right = traversal(curr.right)
|
if not cur:
|
||||||
|
return 2
|
||||||
|
|
||||||
# Case 1:
|
left = self.traversal(cur.left, result)
|
||||||
# 左右节点都有覆盖
|
right = self.traversal(cur.right, result)
|
||||||
if left == 2 and right == 2:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# Case 2:
|
# 情况1: 左右节点都有覆盖
|
||||||
# left == 0 && right == 0 左右节点无覆盖
|
if left == 2 and right == 2:
|
||||||
# left == 1 && right == 0 左节点有摄像头,右节点无覆盖
|
return 0
|
||||||
# left == 0 && right == 1 左节点有无覆盖,右节点摄像头
|
|
||||||
# left == 0 && right == 2 左节点无覆盖,右节点覆盖
|
|
||||||
# left == 2 && right == 0 左节点覆盖,右节点无覆盖
|
|
||||||
elif left == 0 or right == 0:
|
|
||||||
result += 1
|
|
||||||
return 1
|
|
||||||
|
|
||||||
# Case 3:
|
# 情况2:
|
||||||
# left == 1 && right == 2 左节点有摄像头,右节点有覆盖
|
# left == 0 && right == 0 左右节点无覆盖
|
||||||
# left == 2 && right == 1 左节点有覆盖,右节点有摄像头
|
# left == 1 && right == 0 左节点有摄像头,右节点无覆盖
|
||||||
# left == 1 && right == 1 左右节点都有摄像头
|
# left == 0 && right == 1 左节点无覆盖,右节点有摄像头
|
||||||
elif left == 1 or right == 1:
|
# left == 0 && right == 2 左节点无覆盖,右节点覆盖
|
||||||
return 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
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user