Description
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Example 1:
Input: p = [1,2,3], q = [1,2,3] Output: true
Example 2:
Input: p = [1,2], q = [1,null,2] Output: false
Example 3:
Input: p = [1,2,1], q = [1,1,2] Output: false
My Solution
class Solution:
from collections import deque
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
elif (p and q) and (p.val == q.val):
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
else:
return False
'Algorithm > Daily Coding Tests Challenge' 카테고리의 다른 글
[프로그래머스] level2. 가장 큰 수 (0) | 2021.09.15 |
---|---|
[프로그래머스] level2. H-Index (0) | 2021.09.13 |
[Leetcode] Easy: Path Sum (0) | 2021.09.09 |
[프로그래머스] level2. 타겟 넘버 (0) | 2021.09.08 |
[Baekjoon] 그리디 알고리즘 : ATM (0) | 2021.09.07 |