반응형
풀이.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
def isSame(p,q):
if(p==None and q==None): return True
elif(p==None or q == None): return False
elif(p.val!=q.val):
return False
elif(p.val == q.val):
return isSame(p.left,q.left) and isSame(p.right,q.right)
return isSame(p,q)
return isSame(p.left,q.left) and isSame(p.right,q.right)
이 부분의 코드를 통해
if(p==None and q==None): return True
elif(p==None or q == None): return False
elif(p.val!=q.val):
return False
elif(p.val == q.val):
return isSame(p.left,q.left) and isSame(p.right,q.right)
이 코드에서 False가 한번이라도 return 되면 그 상위의 isSame의 리턴 값은 모두 False가 된다.
반응형
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 103. Binary Tree Zigzag Level Order Traversal (0) | 2020.12.31 |
---|---|
[LeetCode][Python3] 104. Maximum Depth of Binary Tree (0) | 2020.12.28 |
[LeetCode][Python3] 102. Binary Tree Level Order Traversal (0) | 2020.12.27 |
[LeetCode][Python3] 98. Validate Binary Search Tree (0) | 2020.12.27 |
[LeetCode][Python3] 17. Letter Combinations of a Phone Number (0) | 2020.12.27 |