mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
rust 968 min camera cover version 2
This commit is contained in:
@ -626,6 +626,42 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 版本二
|
||||||
|
enum NodeState {
|
||||||
|
NoCover = 0,
|
||||||
|
Camera = 1,
|
||||||
|
Covered = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn min_camera_cover(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||||
|
let mut res = 0;
|
||||||
|
let state = Self::traversal(&root, &mut res);
|
||||||
|
match state {
|
||||||
|
NodeState::NoCover => res + 1,
|
||||||
|
_ => res,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn traversal(cur: &Option<Rc<RefCell<TreeNode>>>, ans: &mut i32) -> NodeState {
|
||||||
|
if let Some(node) = cur {
|
||||||
|
let node = node.borrow();
|
||||||
|
let left_state = Self::traversal(&node.left, ans);
|
||||||
|
let right_state = Self::traversal(&node.right, ans);
|
||||||
|
match (left_state, right_state) {
|
||||||
|
(NodeState::NoCover, _) | (_, NodeState::NoCover) => {
|
||||||
|
*ans += 1;
|
||||||
|
NodeState::Camera
|
||||||
|
}
|
||||||
|
(NodeState::Camera, _) | (_, NodeState::Camera) => NodeState::Covered,
|
||||||
|
(_, _) => NodeState::NoCover,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
NodeState::Covered
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
Reference in New Issue
Block a user