-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisSubtree.py
More file actions
29 lines (22 loc) · 775 Bytes
/
Copy pathisSubtree.py
File metadata and controls
29 lines (22 loc) · 775 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
28
29
# Definition for a binary tree node.
from typing import Optional
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:
'''
isSame(p, q):
'''
def isSame(p, q) -> bool:
if not p and not q:
return True
if not p or not q:
return False
if p.val == q.val:
left= isSame(p.left, q.left)
right= isSame(p.right, q.right)
return left and right
return isSame(root, subRoot)