File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ use std:: rc:: Rc ;
2+ use std:: cell:: RefCell ;
3+ impl Solution {
4+ pub fn is_subtree (
5+ root : Option < Rc < RefCell < TreeNode > > > ,
6+ sub_root : Option < Rc < RefCell < TreeNode > > >
7+ ) -> bool {
8+ fn is_sametree ( a : Option < Rc < RefCell < TreeNode > > > , b : Option < Rc < RefCell < TreeNode > > > ) -> bool {
9+ match ( a, b) {
10+ ( None , None ) => true ,
11+ ( Some ( a) , Some ( b) ) => {
12+ a. borrow ( ) . val == b. borrow ( ) . val
13+ && is_sametree ( a. borrow ( ) . left . clone ( ) , b. borrow ( ) . left . clone ( ) )
14+ && is_sametree ( a. borrow ( ) . right . clone ( ) , b. borrow ( ) . right . clone ( ) )
15+ }
16+ _ => false ,
17+ }
18+ }
19+
20+ match ( root, sub_root) {
21+ ( _, None ) => true ,
22+ ( None , _) => false ,
23+ ( Some ( root) , Some ( sub_root) ) => {
24+ if is_sametree ( Some ( root. clone ( ) ) , Some ( sub_root. clone ( ) ) ) {
25+ return true ;
26+ }
27+ Solution :: is_subtree ( root. borrow ( ) . left . clone ( ) , Some ( sub_root. clone ( ) ) )
28+ || Solution :: is_subtree ( root. borrow ( ) . right . clone ( ) , Some ( sub_root) )
29+ }
30+ }
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments