This commit is contained in:
youngyangyang04
2021-12-20 22:44:15 +08:00
parent 0df748cd9a
commit 1c6ad04346
10 changed files with 100 additions and 111 deletions

View File

@@ -5,7 +5,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 968.监控二叉树
# 968.监控二叉树
[力扣题目链接](https://leetcode-cn.com/problems/binary-tree-cameras/)
@@ -19,17 +19,17 @@
![](https://img-blog.csdnimg.cn/20201229175736596.png)
输入:[0,0,null,0,0]
输出1
解释:如图所示,一台摄像头足以监控所有节点。
* 输入:[0,0,null,0,0]
* 输出1
* 解释:如图所示,一台摄像头足以监控所有节点。
示例 2
![](https://img-blog.csdnimg.cn/2020122917584449.png)
输入:[0,0,null,0,null,0,null,null,0]
输出2
解释:需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。
* 输入:[0,0,null,0,null,0,null,null,0]
* 输出2
* 解释:需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。
提示:
@@ -72,17 +72,17 @@
后序遍历代码如下:
```
int traversal(TreeNode* cur) {
int traversal(TreeNode* cur) {
// 空节点,该节点有覆盖
if (终止条件) return ;
// 空节点,该节点有覆盖
if (终止条件) return ;
int left = traversal(cur->left); // 左
int right = traversal(cur->right); // 右
int left = traversal(cur->left); // 左
int right = traversal(cur->right); // 右
逻辑处理 // 中
return ;
}
逻辑处理 // 中
return ;
}
```
**注意在以上代码中我们取了左孩子的返回值右孩子的返回值即left 和 right 以后推导中间节点的状态**
@@ -212,7 +212,7 @@ int minCameraCover(TreeNode* root) {
**以下我的代码注释很详细,为了把情况说清楚,特别把每种情况列出来。**
## C++代码
C++代码如下:
```CPP
// 版本一
@@ -313,7 +313,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
private int count = 0;
@@ -343,14 +343,8 @@ class Solution {
```
Python
### Python
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minCameraCover(self, root: TreeNode) -> int:
# Greedy Algo:
@@ -397,8 +391,9 @@ class Solution:
result += 1
return result
```
Go
```
### Go
```go
const inf = math.MaxInt64 / 2
@@ -427,7 +422,8 @@ func min(a, b int) int {
}
```
Javascript:
### Javascript
```Javascript
var minCameraCover = function(root) {
let result = 0
@@ -464,7 +460,7 @@ var minCameraCover = function(root) {
};
```
C:
### C
```c
/*
**函数后序遍历二叉树。判断一个结点状态时,根据其左右孩子结点的状态进行判断