diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index ec12c525..3cb9d3db 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -375,6 +375,55 @@ object Solution { } ``` +## rust + +递归: + +```rust +impl Solution { + pub fn convert_bst(root: Option>>) -> Option>> { + let mut pre = 0; + Self::traversal(&root, &mut pre); + root + } + + pub fn traversal(cur: &Option>>, pre: &mut i32) { + if cur.is_none() { + return; + } + let mut node = cur.as_ref().unwrap().borrow_mut(); + Self::traversal(&node.right, pre); + *pre += node.val; + node.val = *pre; + Self::traversal(&node.left, pre); + } +} +``` + +迭代: + +```rust +impl Solution { + pub fn convert_bst(root: Option>>) -> Option>> { + let mut cur = root.clone(); + let mut stack = vec![]; + let mut pre = 0; + while !stack.is_empty() || cur.is_some() { + while let Some(node) = cur { + cur = node.borrow().right.clone(); + stack.push(node); + } + if let Some(node) = stack.pop() { + pre += node.borrow().val; + node.borrow_mut().val = pre; + cur = node.borrow().left.clone(); + } + } + root + } +} +``` +

diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index e4a0512a..d897755b 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -577,6 +577,91 @@ object Solution { result } } +``` +### Rust +```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 + } +} + +/// 版本二 +enum NodeState { + NoCover = 0, + Camera = 1, + Covered = 2, +} + +impl Solution { + pub fn min_camera_cover(root: Option>>) -> i32 { + let mut res = 0; + let state = Self::traversal(&root, &mut res); + match state { + NodeState::NoCover => res + 1, + _ => res, + } + } + + pub fn traversal(cur: &Option>>, 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 + } + } +} + ```