Problem :

https://leetcode.com/problems/subtree-of-another-tree/description/


My Solution :

class Solution:
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
if (s and t) is None:
return s is t
return (self.isSameTree(s, t) or
self.isSubtree(s.left, t) or
self.isSubtree(s.right, t))

def isSameTree(self, p, q):
if (p and q) is None:
return p is q
return (p.val == q.val and
self.isSameTree(p.left, q.left) and
self.isSameTree(p.right, q.right))


Comment :

좀 무식한 방법 같기는 한데... 지난번 풀었던 문제의 isSameTree method를 그대로 가져와서 재활용 하였다.


2018/09/26 - [프로그래밍] - [LeetCode][Python3] 100. Same Tree