Skip to content

Commit 3f3767d

Browse files
committed
+ problem 968
1 parent 91f05b7 commit 3f3767d

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 968. Binary Tree Cameras
2+
You are given the `root` of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.
3+
4+
Return *the minimum number of cameras needed to monitor all nodes of the tree*.
5+
6+
#### Example 1:
7+
![](https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png)
8+
<pre>
9+
<strong>Input:</strong> root = [0,0,null,0,0]
10+
<strong>Output:</strong> 1
11+
<strong>Explanation:</strong> One camera is enough to monitor all nodes if placed as shown.
12+
</pre>
13+
14+
#### Example 2:
15+
![](https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png)
16+
<pre>
17+
<strong>Input:</strong> root = [0,0,null,0,null,0,null,null,0]
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong> At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
20+
</pre>
21+
22+
#### Constraints:
23+
* The number of nodes in the tree is in the range `[1, 1000]`.
24+
* `Node.val == 0`
25+
26+
## Solutions (Python)
27+
28+
### 1. Solution
29+
```Python
30+
# Definition for a binary tree node.
31+
# class TreeNode:
32+
# def __init__(self, val=0, left=None, right=None):
33+
# self.val = val
34+
# self.left = left
35+
# self.right = right
36+
37+
from functools import cache
38+
39+
40+
class Solution:
41+
@cache
42+
def minCameraCover(self, root: Optional[TreeNode]) -> int:
43+
if root is None:
44+
return 0
45+
46+
ret = self.coverBySelf(root)
47+
48+
if root.left is not None:
49+
ret = min(ret, self.coverBySelf(root.left) +
50+
self.minCameraCover(root.right))
51+
if root.right is not None:
52+
ret = min(ret, self.coverBySelf(root.right) +
53+
self.minCameraCover(root.left))
54+
55+
return ret
56+
57+
@cache
58+
def coverBySelf(self, root: TreeNode) -> int:
59+
ret = 1
60+
if root.left is not None:
61+
ret += self.coverByParent(root.left)
62+
if root.right is not None:
63+
ret += self.coverByParent(root.right)
64+
65+
return ret
66+
67+
@cache
68+
def coverByParent(self, root: TreeNode) -> int:
69+
return min(self.coverBySelf(root), self.minCameraCover(root.left) + self.minCameraCover(root.right))
70+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 968. 监控二叉树
2+
给定一个二叉树,我们在树的节点上安装摄像头。
3+
4+
节点上的每个摄影头都可以监视**其父对象、自身及其直接子对象**
5+
6+
计算监控树的所有节点所需的最小摄像头数量。
7+
8+
#### 示例 1:
9+
![](https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png)
10+
<pre>
11+
<strong>输入:</strong> root = [0,0,null,0,0]
12+
<strong>输出:</strong> 1
13+
<strong>解释:</strong> 如图所示,一台摄像头足以监控所有节点。
14+
</pre>
15+
16+
#### 示例 2:
17+
![](https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png)
18+
<pre>
19+
<strong>输入:</strong> root = [0,0,null,0,null,0,null,null,0]
20+
<strong>输出:</strong> 2
21+
<strong>解释:</strong> 需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。
22+
</pre>
23+
24+
#### 提示:
25+
1. 给定树的节点数的范围是 `[1, 1000]`
26+
2. 每个节点的值都是 0。
27+
28+
## 题解 (Python)
29+
30+
### 1. 题解
31+
```Python
32+
# Definition for a binary tree node.
33+
# class TreeNode:
34+
# def __init__(self, val=0, left=None, right=None):
35+
# self.val = val
36+
# self.left = left
37+
# self.right = right
38+
39+
from functools import cache
40+
41+
42+
class Solution:
43+
@cache
44+
def minCameraCover(self, root: Optional[TreeNode]) -> int:
45+
if root is None:
46+
return 0
47+
48+
ret = self.coverBySelf(root)
49+
50+
if root.left is not None:
51+
ret = min(ret, self.coverBySelf(root.left) +
52+
self.minCameraCover(root.right))
53+
if root.right is not None:
54+
ret = min(ret, self.coverBySelf(root.right) +
55+
self.minCameraCover(root.left))
56+
57+
return ret
58+
59+
@cache
60+
def coverBySelf(self, root: TreeNode) -> int:
61+
ret = 1
62+
if root.left is not None:
63+
ret += self.coverByParent(root.left)
64+
if root.right is not None:
65+
ret += self.coverByParent(root.right)
66+
67+
return ret
68+
69+
@cache
70+
def coverByParent(self, root: TreeNode) -> int:
71+
return min(self.coverBySelf(root), self.minCameraCover(root.left) + self.minCameraCover(root.right))
72+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
from functools import cache
9+
10+
11+
class Solution:
12+
@cache
13+
def minCameraCover(self, root: Optional[TreeNode]) -> int:
14+
if root is None:
15+
return 0
16+
17+
ret = self.coverBySelf(root)
18+
19+
if root.left is not None:
20+
ret = min(ret, self.coverBySelf(root.left) +
21+
self.minCameraCover(root.right))
22+
if root.right is not None:
23+
ret = min(ret, self.coverBySelf(root.right) +
24+
self.minCameraCover(root.left))
25+
26+
return ret
27+
28+
@cache
29+
def coverBySelf(self, root: TreeNode) -> int:
30+
ret = 1
31+
if root.left is not None:
32+
ret += self.coverByParent(root.left)
33+
if root.right is not None:
34+
ret += self.coverByParent(root.right)
35+
36+
return ret
37+
38+
@cache
39+
def coverByParent(self, root: TreeNode) -> int:
40+
return min(self.coverBySelf(root), self.minCameraCover(root.left) + self.minCameraCover(root.right))

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@
629629
[965][965l] |[Univalued Binary Tree][965] |![py]
630630
[966][966l] |[Vowel Spellchecker][966] |![py]
631631
[967][967l] |[Numbers With Same Consecutive Differences][967] |![rb]&nbsp;&nbsp;![rs]
632+
[968][968l] |[Binary Tree Cameras][968] |![py]
632633
[969][969l] |[Pancake Sorting][969] |![py]
633634
[970][970l] |[Powerful Integers][970] |![rs]
634635
[973][973l] |[K Closest Points to Origin][973] |![rs]
@@ -2167,6 +2168,7 @@
21672168
[965]:Problemset/0965-Univalued%20Binary%20Tree/README.md#965-univalued-binary-tree
21682169
[966]:Problemset/0966-Vowel%20Spellchecker/README.md#966-vowel-spellchecker
21692170
[967]:Problemset/0967-Numbers%20With%20Same%20Consecutive%20Differences/README.md#967-numbers-with-same-consecutive-differences
2171+
[968]:Problemset/0968-Binary%20Tree%20Cameras/README.md#968-binary-tree-cameras
21702172
[969]:Problemset/0969-Pancake%20Sorting/README.md#969-pancake-sorting
21712173
[970]:Problemset/0970-Powerful%20Integers/README.md#970-powerful-integers
21722174
[973]:Problemset/0973-K%20Closest%20Points%20to%20Origin/README.md#973-k-closest-points-to-origin
@@ -3704,6 +3706,7 @@
37043706
[965l]:https://leetcode.com/problems/univalued-binary-tree/
37053707
[966l]:https://leetcode.com/problems/vowel-spellchecker/
37063708
[967l]:https://leetcode.com/problems/numbers-with-same-consecutive-differences/
3709+
[968l]:https://leetcode.com/problems/binary-tree-cameras/
37073710
[969l]:https://leetcode.com/problems/pancake-sorting/
37083711
[970l]:https://leetcode.com/problems/powerful-integers/
37093712
[973l]:https://leetcode.com/problems/k-closest-points-to-origin/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@
629629
[965][965l] |[单值二叉树][965] |![py]
630630
[966][966l] |[元音拼写检查器][966] |![py]
631631
[967][967l] |[连续差相同的数字][967] |![rb]&nbsp;&nbsp;![rs]
632+
[968][968l] |[监控二叉树][968] |![py]
632633
[969][969l] |[煎饼排序][969] |![py]
633634
[970][970l] |[强整数][970] |![rs]
634635
[973][973l] |[最接近原点的 K 个点][973] |![rs]
@@ -2167,6 +2168,7 @@
21672168
[965]:Problemset/0965-Univalued%20Binary%20Tree/README_CN.md#965-单值二叉树
21682169
[966]:Problemset/0966-Vowel%20Spellchecker/README_CN.md#966-元音拼写检查器
21692170
[967]:Problemset/0967-Numbers%20With%20Same%20Consecutive%20Differences/README_CN.md#967-连续差相同的数字
2171+
[968]:Problemset/0968-Binary%20Tree%20Cameras/README_CN.md#968-监控二叉树
21702172
[969]:Problemset/0969-Pancake%20Sorting/README_CN.md#969-煎饼排序
21712173
[970]:Problemset/0970-Powerful%20Integers/README_CN.md#970-强整数
21722174
[973]:Problemset/0973-K%20Closest%20Points%20to%20Origin/README_CN.md#973-最接近原点的-K-个点
@@ -3704,6 +3706,7 @@
37043706
[965l]:https://leetcode.cn/problems/univalued-binary-tree/
37053707
[966l]:https://leetcode.cn/problems/vowel-spellchecker/
37063708
[967l]:https://leetcode.cn/problems/numbers-with-same-consecutive-differences/
3709+
[968l]:https://leetcode.cn/problems/binary-tree-cameras/
37073710
[969l]:https://leetcode.cn/problems/pancake-sorting/
37083711
[970l]:https://leetcode.cn/problems/powerful-integers/
37093712
[973l]:https://leetcode.cn/problems/k-closest-points-to-origin/

0 commit comments

Comments
 (0)