-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_0101_symmetric_tree.rs
More file actions
42 lines (40 loc) · 1.06 KB
/
_0101_symmetric_tree.rs
File metadata and controls
42 lines (40 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
struct Solution;
use crate::util::*;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
if let Some(node) = root {
let node = node.borrow();
return Self::is_mirror(&node.left, &node.right);
}
true
}
fn is_mirror(
node1: &Option<Rc<RefCell<TreeNode>>>,
node2: &Option<Rc<RefCell<TreeNode>>>,
) -> bool {
match (node1, node2) {
(Some(n1), Some(n2)) => {
if n1.borrow().val != n2.borrow().val {
return false;
}
return Self::is_mirror(&n1.borrow().left, &n2.borrow().right)
&& Self::is_mirror(&n1.borrow().right, &n2.borrow().left);
}
(None, None) => true,
_ => false,
}
}
}
#[test]
fn test() {
assert_eq!(
Solution::is_symmetric(tree!(
1,
tree!(2, tree!(3), tree!(4)),
tree!(2, tree!(4), tree!(3))
)),
true
);
}