-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy path572_Subtree_of_Another_Tree.py
More file actions
52 lines (46 loc) · 1.54 KB
/
Copy path572_Subtree_of_Another_Tree.py
File metadata and controls
52 lines (46 loc) · 1.54 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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
# My initial approacch
class Solution(object):
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
startingNodesOnS = []
self.getStartingNodeOnS(s, t.val, startingNodesOnS)
if len(startingNodesOnS) <= 0:
return False
for node in startingNodesOnS:
if self.isSameTree(node, t):
return True
return False
def getStartingNodeOnS(self, s, tVal, startingNodesOnS):
if not s:
return
if s.val == tVal:
startingNodesOnS.append(s)
leftNode = self.getStartingNodeOnS(s.left, tVal, startingNodesOnS)
rightNode = self.getStartingNodeOnS(s.right, tVal, startingNodesOnS)
if leftNode:
startingNodesOnS.append(leftNode)
return
elif rightNode:
startingNodesOnS.append(rightNode)
return
else:
return
def isSameTree(self, startingNodeOnS, t):
if not startingNodeOnS and not t:
return True
if startingNodeOnS and t and startingNodeOnS.val == t.val:
left = self.isSameTree(startingNodeOnS.left, t.left)
right = self.isSameTree(startingNodeOnS.right, t.right)
return left and right
else:
return False