mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
rust 968 min camera cover
This commit is contained in:
@ -579,6 +579,55 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
/// 版本一
|
||||||
|
impl Solution {
|
||||||
|
pub fn min_camera_cover(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||||
|
let mut res = 0;
|
||||||
|
if Self::traversal(&root, &mut res) == 0 {
|
||||||
|
res += 1;
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn traversal(cur: &Option<Rc<RefCell<TreeNode>>>, ans: &mut i32) -> i32 {
|
||||||
|
// 0 未覆盖 1 节点已设置摄像头 2 节点已覆盖
|
||||||
|
if let Some(node) = cur {
|
||||||
|
let node = node.borrow();
|
||||||
|
|
||||||
|
let left = Self::traversal(&node.left, ans);
|
||||||
|
let right = Self::traversal(&node.right, ans);
|
||||||
|
|
||||||
|
// 左右节点都被覆盖
|
||||||
|
if left == 2 && right == 2 {
|
||||||
|
return 0; // 无覆盖
|
||||||
|
}
|
||||||
|
|
||||||
|
// left == 0 right == 0 左右无覆盖
|
||||||
|
// left == 0 right == 1 左节点无覆盖 右节点有摄像头
|
||||||
|
// left == 1 right == 0 左节点有摄像头 左节点无覆盖
|
||||||
|
// left == 0 right == 2 左节点无覆盖 右节点有覆盖
|
||||||
|
// left == 2 right == 0 左节点有覆盖 右节点无覆盖
|
||||||
|
if left == 0 || right == 0 {
|
||||||
|
*ans += 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// left == 1 right == 1 左节点有摄像头 右节点有摄像头
|
||||||
|
// left == 1 right == 2 左节点有摄像头 右节点覆盖
|
||||||
|
// left == 2 right == 1 左节点覆盖 右节点有摄像头
|
||||||
|
if left == 1 || right == 1 {
|
||||||
|
return 2; // 已覆盖
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
-1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user