문제
Given two binary trees, 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: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
Output: true
Example 2:
Input: 1 1
/ \
2 2
[1,2], [1,null,2]
Output: false
Example 3:
Input: 1 1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]
Output: false
풀이
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if p==None and q==None :
return True
if p==None or q==None :
return False
return (p.val==q.val) and (self.isSameTree(p.left,q.left)) and (self.isSameTree(p.right,q.right))
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
66. Plus One (0) | 2021.03.04 |
---|---|
2. Add Two Numbers (0) | 2020.05.11 |
88. Merge Sorted Array (0) | 2020.05.11 |
21. Merge Two Sorted Lists (0) | 2020.05.09 |
83. Remove Duplicates from Sorted List (0) | 2020.05.09 |