diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 37adfd54..3bdd862f 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -826,6 +826,67 @@ object Solution { } ``` +## Rust + +递归: +```rust +impl Solution { + pub fn is_symmetric(root: Option>>) -> bool { + Self::recur( + &root.as_ref().unwrap().borrow().left, + &root.as_ref().unwrap().borrow().right, + ) + } + pub fn recur( + left: &Option>>, + right: &Option>>, + ) -> 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>>) -> 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 + } +} +``` +

diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index be53c417..ee7bd50e 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -200,32 +200,6 @@ public: }; ``` -rust: -```rust -impl Solution { - pub fn max_depth(root: Option>>) -> 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>>) -> 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>>) -> 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 + } +``` +