-
Notifications
You must be signed in to change notification settings - Fork 31
/
572.py
28 lines (26 loc) · 753 Bytes
/
572.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
27
28
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
if not s:
return False
if self.isSame(s, t):
return True
return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
def isSame(self, s, t):
if not s and not t:
return True
if not s or not t:
return False
if s.val != t.val:
return False
return self.isSame(s.left, t.left) and self.isSame(s.right, t.right)