mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Update 0101.对称二叉树.md
This commit is contained in:
@ -800,6 +800,39 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
迭代:
|
||||||
|
```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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user