-
Notifications
You must be signed in to change notification settings - Fork 2
/
0100-same-tree.rs
37 lines (32 loc) · 959 Bytes
/
0100-same-tree.rs
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
mod tree;
use tree::*;
use std::cell::RefCell;
use std::rc::Rc;
struct Solution;
impl Solution {
pub fn is_same_tree(
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> bool {
match (p, q) {
(None, None) => true,
(Some(ref p), Some(ref q)) => {
p.borrow().val == q.borrow().val
&& Self::is_same_tree(p.borrow().left.clone(), q.borrow().left.clone())
&& Self::is_same_tree(p.borrow().right.clone(), q.borrow().right.clone())
}
_ => false,
}
}
}
fn main() {
let p = tree![1, 2, 3];
let q = tree![1, 2, 3];
assert_eq!(true, Solution::is_same_tree(p, q));
let p = tree![1, 2];
let q = tree![1, null, 2];
assert_eq!(false, Solution::is_same_tree(p, q));
let p = tree![1, 2, 1];
let q = tree![1, 1, 2];
assert_eq!(false, Solution::is_same_tree(p, q));
}