Skip to content

Commit 093a533

Browse files
committed
Leetcode Accepted
1 parent 4584a7c commit 093a533

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Subtree of Another Tree
2+
# https://leetcode.com/problems/subtree-of-another-tree/
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode(object):
6+
# def __init__(self, val=0, left=None, right=None):
7+
# self.val = val
8+
# self.left = left
9+
# self.right = right
10+
class Solution(object):
11+
def sameTrees(self, t1, t2):
12+
# if we reach null node of both trees then return true
13+
if (not t1 and not t2):
14+
return True
15+
# if one tree has nodes, but another doesn't we return false
16+
if ((not t1) and t2):
17+
return False
18+
if (t1 and (not t2)):
19+
return False
20+
# if left sub tree is not the same we return False and early terminate.
21+
if not(self.sameTrees(t1.left, t2.left)):
22+
return False
23+
# if values are not same we reutrn false and early terminate without checking further.
24+
if (t1.val != t2.val):
25+
return False
26+
# if right sub tree is not the same we return false and early terminate
27+
if not(self.sameTrees(t1.right, t2.right)):
28+
return False
29+
# else we return true to keep looking further.
30+
return True
31+
32+
def findSubTree(self, root, subRoot):
33+
# if it's a null node, we return back.
34+
if not root:
35+
return
36+
# if we find current node value = subTree root value we check if they are same or not
37+
if (root.val == subRoot.val):
38+
if (self.sameTrees(root, subRoot)):
39+
return True
40+
# if sub tree found in left sub tree we return true and early terminate.
41+
if (self.findSubTree(root.left, subRoot)):
42+
return True
43+
# if sub tree found in right sub tree we return true and early teminate
44+
if (self.findSubTree(root.right, subRoot)):
45+
return True
46+
47+
def isSubtree(self, root, subRoot):
48+
return self.findSubTree(root, subRoot)
49+
"""
50+
:type root: TreeNode
51+
:type subRoot: TreeNode
52+
:rtype: bool
53+
"""

Blind 75/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@
255255
<li><a href="Programs/Serialize and Deserialize Binary Tree - LevelOrder Toomuch Space.py">Too much space with level order</a>: This exceeded leetcode space limit. </li>
256256
</ul>
257257
</li>
258-
<li>Subtree of Another Tree</li>
258+
<li><a href="Programs/Subtree of Another Tree.py">Subtree of Another Tree</a>
259+
<p><b>Approach</b>: Recursive function similar to inorder traversal that checks if 2 sub trees are same or not.
260+
Another function that checks if the current node in the larger tree is same as the root of sub tree, then it calls the previous function to check if the subtrees are same or not.
261+
</p>
262+
</li>
259263
<li>Construct Binary Tree from Preorder and Inorder Traversal</li>
260264
<li>Validate Binary Search Tree</li>
261265
<li>Kth Smallest Element in a BST</li>

0 commit comments

Comments
 (0)