-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtree_of_another_tree.rs
82 lines (72 loc) · 2.53 KB
/
subtree_of_another_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::tree_node::TreeNode;
use std::rc::Rc;
use std::cell::RefCell;
/// Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree
/// of `root` with the same structure and node values of `subRoot` and `false` otherwise.
///
/// A subtree of a binary tree `tree` is a tree that consists of a node in `tree` and all of this
/// node's descendants. The tree `tree` could also be considered as a subtree of itself.
struct Solution;
impl Solution {
fn matches(root: &Option<Rc<RefCell<TreeNode>>>, sub_root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
match (root, sub_root) {
(Some(rc_root), Some(rc_sub_root)) => {
let node_root = rc_root.borrow();
let node_sub_root = rc_sub_root.borrow();
let mut result = node_root.val == node_sub_root.val;
if result {
result = Self::matches(&node_root.left, &node_sub_root.left);
}
if result {
result = Self::matches(&node_root.right, &node_sub_root.right);
}
result
}
(None, None) => {
true
}
(_, _) => {
false
}
}
}
fn is_subtree_worker(root: &Option<Rc<RefCell<TreeNode>>>, sub_root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
let mut result = Self::matches(root, sub_root);
if !result {
match root {
Some(rc) => {
let node = rc.borrow();
result = Self::is_subtree_worker(&node.left, sub_root);
if !result {
result = Self::is_subtree_worker(&node.right, sub_root);
}
}
None => {
// do nothing
}
}
}
result
}
pub fn is_subtree(root: Option<Rc<RefCell<TreeNode>>>, sub_root: Option<Rc<RefCell<TreeNode>>>) -> bool {
Self::is_subtree_worker(&root, &sub_root)
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let root = tree!("[3,4,5,1,2]");
let sub_root = tree!("[4,1,2]");
let result = Solution::is_subtree(root, sub_root);
assert!(result);
}
#[test]
fn example_2() {
let root = tree!("[3,4,5,1,2,null,null,null,null,0]");
let sub_root = tree!("[4,1,2]");
let result = Solution::is_subtree(root, sub_root);
assert!(!result);
}
}