File tree Expand file tree Collapse file tree 1 file changed +8
-12
lines changed
572-subtree-of-another-tree Expand file tree Collapse file tree 1 file changed +8
-12
lines changed Original file line number Diff line number Diff line change 7
7
class Solution :
8
8
def isSubtree (self , root : Optional [TreeNode ], subRoot : Optional [TreeNode ]) -> bool :
9
9
10
-
11
-
12
- def helper (p ,q ):
10
+ def sameTree (p ,q ):
13
11
if not p and q :
14
12
return False
15
13
if p and not q :
16
14
return False
17
15
if not p and not q :
18
16
return True
19
-
20
17
if p .val != q .val :
21
18
return False
22
19
23
- left = helper (p .left , q .left )
24
- right = helper (p .right , q .right )
25
-
20
+ left = sameTree (p .left , q .left )
21
+ right = sameTree (p .right , q .right )
26
22
return left and right
27
-
28
- if not root and subRoot :
29
- return False
23
+
30
24
if root and not subRoot :
25
+ return True
26
+ if not root and subRoot :
31
27
return False
32
- if helper (root ,subRoot ):
28
+ if sameTree (root , subRoot ):
33
29
return True
34
30
35
- return self .isSubtree (root .left ,subRoot ) or self .isSubtree (root .right , subRoot )
31
+ return self .isSubtree (root .left , subRoot ) or self .isSubtree (root .right , subRoot )
36
32
You can’t perform that action at this time.
0 commit comments