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:
@ -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">
|
<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