Skip to content

Commit 9b69f8c

Browse files
authoredDec 26, 2021
Create 572-Subtree-of-Another-Tree.py
1 parent 0cf5ca0 commit 9b69f8c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
 

‎572-Subtree-of-Another-Tree.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
9+
if not t: return True
10+
if not s: return False
11+
12+
if self.sameTree(s, t):
13+
return True
14+
return (self.isSubtree(s.left, t) or
15+
self.isSubtree(s.right, t))
16+
17+
def sameTree(self, s, t):
18+
if not s and not t:
19+
return True
20+
if s and t and s.val == t.val:
21+
return (self.sameTree(s.left, t.left) and
22+
self.sameTree(s.right, t.right))
23+
return False

0 commit comments

Comments
 (0)
Please sign in to comment.