Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-backtracking

This commit is contained in:
Yuhao Ju
2022-12-09 21:25:00 +08:00
committed by GitHub
2 changed files with 122 additions and 0 deletions

View File

@ -655,6 +655,78 @@ object Solution {
}
```
### rust
递归:
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn merge_trees(
root1: Option<Rc<RefCell<TreeNode>>>,
root2: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
if root1.is_none() {
return root2;
}
if root2.is_none() {
return root1;
}
let binding = root1.clone();
let mut node1 = binding.as_ref().unwrap().borrow_mut();
let node2 = root2.as_ref().unwrap().borrow_mut();
node1.left = Self::merge_trees(node1.left.clone(), node2.left.clone());
node1.right = Self::merge_trees(node1.right.clone(), node2.right.clone());
node1.val += node2.val;
root1
}
}
```
迭代:
```rust
impl Solution {
pub fn merge_trees(
root1: Option<Rc<RefCell<TreeNode>>>,
root2: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
if root1.is_none() {
return root2;
}
if root2.is_none() {
return root1;
}
let mut stack = vec![];
stack.push(root2);
stack.push(root1.clone());
while !stack.is_empty() {
let node1 = stack.pop().unwrap().unwrap();
let node2 = stack.pop().unwrap().unwrap();
let mut node1 = node1.borrow_mut();
let node2 = node2.borrow();
node1.val += node2.val;
if node1.left.is_some() && node2.left.is_some() {
stack.push(node2.left.clone());
stack.push(node1.left.clone());
}
if node1.right.is_some() && node2.right.is_some() {
stack.push(node2.right.clone());
stack.push(node1.right.clone());
}
if node1.left.is_none() && node2.left.is_some() {
node1.left = node2.left.clone();
}
if node1.right.is_none() && node2.right.is_some() {
node1.right = node2.right.clone();
}
}
root1
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -414,6 +414,56 @@ object Solution {
}
```
### rust
递归:
```rust
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn search_bst(
root: Option<Rc<RefCell<TreeNode>>>,
val: i32,
) -> Option<Rc<RefCell<TreeNode>>> {
if root.is_none() || root.as_ref().unwrap().borrow().val == val {
return root;
}
let node_val = root.as_ref().unwrap().borrow().val;
if node_val > val {
return Self::search_bst(root.as_ref().unwrap().borrow().left.clone(), val);
}
if node_val < val {
return Self::search_bst(root.unwrap().borrow().right.clone(), val);
}
None
}
}
```
迭代:
```rust
use std::cell::RefCell;
use std::rc::Rc;
use std::cmp;
impl Solution {
pub fn search_bst(
mut root: Option<Rc<RefCell<TreeNode>>>,
val: i32,
) -> Option<Rc<RefCell<TreeNode>>> {
while let Some(ref node) = root.clone() {
match val.cmp(&node.borrow().val) {
cmp::Ordering::Less => root = node.borrow().left.clone(),
cmp::Ordering::Equal => return root,
cmp::Ordering::Greater => root = node.borrow().right.clone(),
};
}
None
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>