更新图床
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
|
||||
<img src="../pics/训练营.png" width="1000"/>
|
||||
@@ -5,6 +6,7 @@
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
|
||||
# 968.监控二叉树
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/binary-tree-cameras/)
|
||||
@@ -17,7 +19,7 @@
|
||||
|
||||
示例 1:
|
||||
|
||||

|
||||

|
||||
|
||||
* 输入:[0,0,null,0,0]
|
||||
* 输出:1
|
||||
@@ -25,7 +27,7 @@
|
||||
|
||||
示例 2:
|
||||
|
||||

|
||||

|
||||
|
||||
* 输入:[0,0,null,0,null,0,null,null,0]
|
||||
* 输出:2
|
||||
@@ -139,7 +141,7 @@ if (cur == NULL) return 2;
|
||||
|
||||
如图:
|
||||
|
||||

|
||||

|
||||
|
||||
代码如下:
|
||||
|
||||
@@ -163,6 +165,7 @@ if (left == 2 && right == 2) return 0;
|
||||
此时摄像头的数量要加一,并且return 1,代表中间节点放摄像头。
|
||||
|
||||
代码如下:
|
||||
|
||||
```CPP
|
||||
if (left == 0 || right == 0) {
|
||||
result++;
|
||||
@@ -186,7 +189,7 @@ if (left == 1 || right == 1) return 2;
|
||||
|
||||
**从这个代码中,可以看出,如果left == 1, right == 0 怎么办?其实这种条件在情况2中已经判断过了**,如图:
|
||||
|
||||

|
||||

|
||||
|
||||
这种情况也是大多数同学容易迷惑的情况。
|
||||
|
||||
@@ -194,7 +197,7 @@ if (left == 1 || right == 1) return 2;
|
||||
|
||||
以上都处理完了,递归结束之后,可能头结点 还有一个无覆盖的情况,如图:
|
||||
|
||||

|
||||

|
||||
|
||||
所以递归结束之后,还要判断根节点,如果没有覆盖,result++,代码如下:
|
||||
|
||||
@@ -311,7 +314,8 @@ public:
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
### Java
|
||||
### Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
int res=0;
|
||||
@@ -324,32 +328,32 @@ class Solution {
|
||||
}
|
||||
/**
|
||||
节点的状态值:
|
||||
0 表示无覆盖
|
||||
0 表示无覆盖
|
||||
1 表示 有摄像头
|
||||
2 表示有覆盖
|
||||
2 表示有覆盖
|
||||
后序遍历,根据左右节点的情况,来判读 自己的状态
|
||||
*/
|
||||
public int minCame(TreeNode root){
|
||||
if(root==null){
|
||||
// 空节点默认为 有覆盖状态,避免在叶子节点上放摄像头
|
||||
// 空节点默认为 有覆盖状态,避免在叶子节点上放摄像头
|
||||
return 2;
|
||||
}
|
||||
int left=minCame(root.left);
|
||||
int right=minCame(root.right);
|
||||
|
||||
|
||||
// 如果左右节点都覆盖了的话, 那么本节点的状态就应该是无覆盖,没有摄像头
|
||||
if(left==2&&right==2){
|
||||
//(2,2)
|
||||
//(2,2)
|
||||
return 0;
|
||||
}else if(left==0||right==0){
|
||||
// 左右节点都是无覆盖状态,那 根节点此时应该放一个摄像头
|
||||
// (0,0) (0,1) (0,2) (1,0) (2,0)
|
||||
// (0,0) (0,1) (0,2) (1,0) (2,0)
|
||||
// 状态值为 1 摄像头数 ++;
|
||||
res++;
|
||||
return 1;
|
||||
}else{
|
||||
// 左右节点的 状态为 (1,1) (1,2) (2,1) 也就是左右节点至少存在 1个摄像头,
|
||||
// 那么本节点就是处于被覆盖状态
|
||||
// 那么本节点就是处于被覆盖状态
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
@@ -357,7 +361,8 @@ class Solution {
|
||||
```
|
||||
|
||||
|
||||
### Python
|
||||
### Python
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def minCameraCover(self, root: TreeNode) -> int:
|
||||
@@ -367,19 +372,19 @@ class Solution:
|
||||
# 0: 该节点未覆盖
|
||||
# 1: 该节点有摄像头
|
||||
# 2: 该节点有覆盖
|
||||
|
||||
|
||||
result = 0
|
||||
# 从下往上遍历:后序(左右中)
|
||||
def traversal(curr: TreeNode) -> int:
|
||||
nonlocal result
|
||||
|
||||
|
||||
if not curr: return 2
|
||||
left = traversal(curr.left)
|
||||
right = traversal(curr.right)
|
||||
|
||||
# Case 1:
|
||||
# 左右节点都有覆盖
|
||||
if left == 2 and right == 2:
|
||||
if left == 2 and right == 2:
|
||||
return 0
|
||||
|
||||
# Case 2:
|
||||
@@ -388,7 +393,7 @@ class Solution:
|
||||
# left == 0 && right == 1 左节点有无覆盖,右节点摄像头
|
||||
# left == 0 && right == 2 左节点无覆盖,右节点覆盖
|
||||
# left == 2 && right == 0 左节点覆盖,右节点无覆盖
|
||||
elif left == 0 or right == 0:
|
||||
elif left == 0 or right == 0:
|
||||
result += 1
|
||||
return 1
|
||||
|
||||
@@ -398,16 +403,17 @@ class Solution:
|
||||
# left == 1 && right == 1 左右节点都有摄像头
|
||||
elif left == 1 or right == 1:
|
||||
return 2
|
||||
|
||||
|
||||
# 其他情况前段代码均已覆盖
|
||||
|
||||
if traversal(root) == 0:
|
||||
result += 1
|
||||
|
||||
|
||||
return result
|
||||
```
|
||||
|
||||
### Go
|
||||
### Go
|
||||
|
||||
```go
|
||||
const inf = math.MaxInt64 / 2
|
||||
|
||||
@@ -437,7 +443,8 @@ func min(a, b int) int {
|
||||
|
||||
```
|
||||
|
||||
### Javascript
|
||||
### Javascript
|
||||
|
||||
```Javascript
|
||||
var minCameraCover = function(root) {
|
||||
let result = 0
|
||||
@@ -470,7 +477,7 @@ var minCameraCover = function(root) {
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
|
||||
};
|
||||
```
|
||||
|
||||
@@ -501,7 +508,7 @@ function minCameraCover(root: TreeNode | null): number {
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
### C
|
||||
|
||||
```c
|
||||
/*
|
||||
@@ -576,7 +583,9 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
|
||||
```Rust
|
||||
/// 版本一
|
||||
impl Solution {
|
||||
|
||||
Reference in New Issue
Block a user