100. Same Tree

Doyeon Kim·2022년 5월 21일

코딩테스트 공부

목록 보기
64/171

문제 링크 : 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.

profile
성장하고 도전하는 개발자. 프로그래밍 좋아하세요?

0개의 댓글