Skip to content

Commit bd52437

Browse files
author
Partho Biswas
committed
572. Subtree of Another Tree
1 parent d3402e0 commit bd52437

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
417417
|31| **[114. Flatten Binary Tree to Linked List](https://tinyurl.com/yyc6ol5w)**| [Python](https://tinyurl.com/wu6rdaw/114_Flatten_Binary_Tree_to_Linked_List.py)| [Art 1](https://tinyurl.com/v5s5axv) | Medium | Classic Prolem |
418418
|32| **[297. Serialize and Deserialize Binary Tree](https://tinyurl.com/ycfugzww)**| [Python](https://tinyurl.com/wu6rdaw/297_Serialize_and_Deserialize_Binary_Tree.py), [Swift](https://tinyurl.com/wuja3c4/297_Serialize_and_Deserialize_Binary_Tree.swift) | [Vid 1](https://tinyurl.com/yx4w25u4) | Hard | Classic Prolem |
419419
|33| **[993. Cousins in Binary Tree](https://tinyurl.com/ybtzjhnb)**| [Python](https://tinyurl.com/wu6rdaw/993_Cousins_in_Binary_Tree.py), [Swift](https://tinyurl.com/wuja3c4/993_Cousins_in_Binary_Tree.swift) | | Easy(Not so) | |
420+
|34| **[572. Subtree of Another Tree](https://tinyurl.com/ybeerky9)**| [Python](https://tinyurl.com/wu6rdaw/572_Subtree_of_Another_Tree.py), [Swift](https://tinyurl.com/wuja3c4/572_Subtree_of_Another_Tree.swift) | [Art 1](https://tinyurl.com/y9epsrfa) | Easy(Not so) | |
420421

421422
</p>
422423
</details>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
8+
# My initial approacch
9+
class Solution(object):
10+
def isSubtree(self, s, t):
11+
"""
12+
:type s: TreeNode
13+
:type t: TreeNode
14+
:rtype: bool
15+
"""
16+
startingNodesOnS = []
17+
self.getStartingNodeOnS(s, t.val, startingNodesOnS)
18+
if len(startingNodesOnS) <= 0:
19+
return False
20+
21+
for node in startingNodesOnS:
22+
if self.isSameTree(node, t):
23+
return True
24+
return False
25+
26+
def getStartingNodeOnS(self, s, tVal, startingNodesOnS):
27+
if not s:
28+
return
29+
if s.val == tVal:
30+
startingNodesOnS.append(s)
31+
leftNode = self.getStartingNodeOnS(s.left, tVal, startingNodesOnS)
32+
rightNode = self.getStartingNodeOnS(s.right, tVal, startingNodesOnS)
33+
if leftNode:
34+
startingNodesOnS.append(leftNode)
35+
return
36+
elif rightNode:
37+
startingNodesOnS.append(rightNode)
38+
return
39+
else:
40+
return
41+
42+
def isSameTree(self, startingNodeOnS, t):
43+
if not startingNodeOnS and not t:
44+
return True
45+
if startingNodeOnS and t and startingNodeOnS.val == t.val:
46+
left = self.isSameTree(startingNodeOnS.left, t.left)
47+
right = self.isSameTree(startingNodeOnS.right, t.right)
48+
return left and right
49+
else:
50+
return False
51+
52+

0 commit comments

Comments
 (0)