mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-18 11:13:39 +08:00
Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-dp
This commit is contained in:
@ -375,6 +375,55 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## rust
|
||||
|
||||
递归:
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn convert_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
let mut pre = 0;
|
||||
Self::traversal(&root, &mut pre);
|
||||
root
|
||||
}
|
||||
|
||||
pub fn traversal(cur: &Option<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
@ -577,6 +577,91 @@ object Solution {
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
### Rust
|
||||
```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
|
||||
}
|
||||
}
|
||||
|
||||
/// 版本二
|
||||
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">
|
||||
|
Reference in New Issue
Block a user