-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79.py
26 lines (21 loc) · 804 Bytes
/
79.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
# 572. SubTree of another Tree
# EASY
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSubTreeForNode(self, root, subTree):
if not root and not subTree:
return True
elif not root or not subTree:
return False
else:
return root.val == subTree.val and self.isSubTreeForNode(root.left, subTree.left) and self.isSubTreeForNode(
root.right, subTree.right)
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
if not s:
return False
return self.isSubTreeForNode(s, t) or self.isSubtree(s.left, t) or self.isSubtree(s.right, t)