[LeetCode][Python3] 572. Subtree of Another Tree
2018. 10. 7. 23:57 |
프로그래밍/LeetCode
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를 그대로 가져와서 재활용 하였다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 66. Plus One (0) | 2018.10.15 |
---|---|
[LeetCode][Python3] 543. Diameter of Binary Tree (0) | 2018.10.09 |
[LeetCode][Python3] 617. Merge Two Binary Trees (0) | 2018.10.08 |
[LeetCode][Python3] 581. Shortest Unsorted Continuous Subarray (0) | 2018.10.08 |
[LeetCode][Python3] 538. Convert BST to Greater Tree (0) | 2018.10.07 |
[LeetCode][Python3] 438. Find All Anagrams in a String (0) | 2018.10.07 |
[LeetCode][Python3] 437. Path Sum III (0) | 2018.10.07 |
[LeetCode][Python3] 283. Move Zeroes (0) | 2018.10.05 |
최근에 달린 댓글 최근에 달린 댓글