mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into leetcode-modify-the-code-of-the-string-part
This commit is contained in:
@ -826,6 +826,67 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Rust
|
||||
|
||||
递归:
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
|
||||
Self::recur(
|
||||
&root.as_ref().unwrap().borrow().left,
|
||||
&root.as_ref().unwrap().borrow().right,
|
||||
)
|
||||
}
|
||||
pub fn recur(
|
||||
left: &Option<Rc<RefCell<TreeNode>>>,
|
||||
right: &Option<Rc<RefCell<TreeNode>>>,
|
||||
) -> bool {
|
||||
match (left, right) {
|
||||
(None, None) => true,
|
||||
(Some(n1), Some(n2)) => {
|
||||
return n1.borrow().val == n2.borrow().val
|
||||
&& Self::recur(&n1.borrow().left, &n2.borrow().right)
|
||||
&& Self::recur(&n1.borrow().right, &n2.borrow().left)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
迭代:
|
||||
```rust
|
||||
use std::cell::RefCell;
|
||||
use std::collections::VecDeque;
|
||||
use std::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
|
||||
let mut queue = VecDeque::new();
|
||||
if let Some(node) = root {
|
||||
queue.push_back(node.borrow().left.clone());
|
||||
queue.push_back(node.borrow().right.clone());
|
||||
}
|
||||
while !queue.is_empty() {
|
||||
let (n1, n2) = (queue.pop_front().unwrap(), queue.pop_front().unwrap());
|
||||
match (n1.clone(), n2.clone()) {
|
||||
(None, None) => continue,
|
||||
(Some(n1), Some(n2)) => {
|
||||
if n1.borrow().val != n2.borrow().val {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_ => return false,
|
||||
};
|
||||
queue.push_back(n1.as_ref().unwrap().borrow().left.clone());
|
||||
queue.push_back(n2.as_ref().unwrap().borrow().right.clone());
|
||||
queue.push_back(n1.unwrap().borrow().right.clone());
|
||||
queue.push_back(n2.unwrap().borrow().left.clone());
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -200,32 +200,6 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
rust:
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
if root.is_none(){
|
||||
return 0;
|
||||
}
|
||||
let mut max_depth: i32 = 0;
|
||||
let mut stack = vec![root.unwrap()];
|
||||
while !stack.is_empty() {
|
||||
let num = stack.len();
|
||||
for _i in 0..num{
|
||||
let top = stack.remove(0);
|
||||
if top.borrow_mut().left.is_some(){
|
||||
stack.push(top.borrow_mut().left.take().unwrap());
|
||||
}
|
||||
if top.borrow_mut().right.is_some(){
|
||||
stack.push(top.borrow_mut().right.take().unwrap());
|
||||
}
|
||||
}
|
||||
max_depth+=1;
|
||||
}
|
||||
max_depth
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
那么我们可以顺便解决一下n叉树的最大深度问题
|
||||
|
||||
@ -975,6 +949,50 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## rust
|
||||
### 0104.二叉树的最大深度
|
||||
|
||||
递归:
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
if root.is_none() {
|
||||
return 0;
|
||||
}
|
||||
std::cmp::max(
|
||||
Self::max_depth(root.clone().unwrap().borrow().left.clone()),
|
||||
Self::max_depth(root.unwrap().borrow().right.clone()),
|
||||
) + 1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
迭代:
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
if root.is_none(){
|
||||
return 0;
|
||||
}
|
||||
let mut max_depth: i32 = 0;
|
||||
let mut stack = vec![root.unwrap()];
|
||||
while !stack.is_empty() {
|
||||
let num = stack.len();
|
||||
for _i in 0..num{
|
||||
let top = stack.remove(0);
|
||||
if top.borrow_mut().left.is_some(){
|
||||
stack.push(top.borrow_mut().left.take().unwrap());
|
||||
}
|
||||
if top.borrow_mut().right.is_some(){
|
||||
stack.push(top.borrow_mut().right.take().unwrap());
|
||||
}
|
||||
}
|
||||
max_depth+=1;
|
||||
}
|
||||
max_depth
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
Reference in New Issue
Block a user