Update 0101.对称二叉树.md

This commit is contained in:
fw_qaq
2022-11-13 17:43:14 +08:00
committed by GitHub
parent b56bc32a23
commit 7c97e9b7d2

View File

@ -772,6 +772,34 @@ 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,
}
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>