diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index e4a0512a..4a8f9868 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -579,6 +579,55 @@ object Solution { } ``` +```Rust +/// 版本一 +impl Solution { + pub fn min_camera_cover(root: Option>>) -> i32 { + let mut res = 0; + if Self::traversal(&root, &mut res) == 0 { + res += 1; + } + res + } + + pub fn traversal(cur: &Option>>, 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 + } +} + +``` +