문제 링크 : https://leetcode.com/problems/same-tree/
두 개의 binary tree p와 q가 주어질 때, 둘이 같으면 true를 반환하는 문제이다.
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p and q:
return p.val == q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right, q.right)
return p is q
오늘 풀면서 안 사실 :
return p is q
= True if p==None and q==None else False.
Runtime: 31 ms, faster than 87.92% of Python3 online submissions for Same Tree.
Memory Usage: 13.8 MB, less than 75.09% of Python3 online submissions for Same Tree.