-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path289.py
More file actions
27 lines (21 loc) · 777 Bytes
/
289.py
File metadata and controls
27 lines (21 loc) · 777 Bytes
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if subRoot == None:
return True
if root == None:
return False
if self.same(root, subRoot):
return True
return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)
def same(self, r, s):
if r == None and s == None:
return True
if r and s and r.val == s.val:
return self.same(r.right, s.right) and self.same(r.left, s.left)
return False