-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_subtree.py
36 lines (32 loc) · 994 Bytes
/
is_subtree.py
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
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def isSameTree(self, p, q): #!RECURSION CODE
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if not p and not q:
return True
if not p or not q or p.val != q.val:
return False
return(self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right))
def isSubtree(self, root, subRoot):
"""
:type root: TreeNode
:type subRoot: TreeNode
:rtype: bool
"""
if not subRoot :
return True
if not root:
return False
if self.isSameTree(root,subRoot):
return True
else:
return(self.isSubtree(root.left,subRoot) or self.isSubtree(root.right,subRoot))