-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsame-tree.rs
More file actions
43 lines (39 loc) · 1.01 KB
/
same-tree.rs
File metadata and controls
43 lines (39 loc) · 1.01 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
43
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn is_same_tree(p: Option<Rc<RefCell<TreeNode>>>, q: Option<Rc<RefCell<TreeNode>>>) -> bool {
if p.is_some() != q.is_some() {
return false;
}
if p.is_none() {
return true;
}
let pn = p.unwrap();
let qn = q.unwrap();
let pb = pn.borrow();
let qb = qn.borrow();
if pb.val == qb.val {
Self::is_same_tree(pb.left.clone(), qb.left.clone()) && Self::is_same_tree(pb.right.clone(), qb.right.clone())
} else {
false
}
}
}