일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Convert Sorted List to Binary Search Tree
- Python Code
- 109. Convert Sorted List to Binary Search Tree
- Regular Expression
- Python
- 시바견
- 운영체제
- concurrency
- LeetCode
- Generator
- Decorator
- 파이썬
- 30. Substring with Concatenation of All Words
- iterator
- Substring with Concatenation of All Words
- data science
- 프로그래머스
- Class
- DWG
- Python Implementation
- attribute
- Protocol
- 43. Multiply Strings
- 315. Count of Smaller Numbers After Self
- kaggle
- 715. Range Module
- t1
- 밴픽
- 컴퓨터의 구조
- shiba
Archives
- Today
- Total
Scribbling
LeetCode: 100. Same Tree 본문
BFS로 순회해보자
# 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
from collections import deque
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p == None and q == None:
return True
if p == None and q != None:
return False
if p != None and q == None:
return False
q1, q2 = deque([p]), deque([q])
while q1:
n1 = q1.popleft()
n2 = q2.popleft()
if n1.val != n2.val:
return False
if n1.left != None:
if n2.left == None:
return False
q1.append(n1.left)
q2.append(n2.left)
else:
if n2.left != None:
return False
if n1.right != None:
if n1.right == None:
return False
q1.append(n1.right)
q2.append(n2.right)
else:
if n2.right != None:
return False
return True
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 93. Restore IP Addresses (0) | 2021.10.11 |
---|---|
LeetCode: 91. Decode Ways (0) | 2021.10.11 |
LeetCode: 90. Subsets II (0) | 2021.10.10 |
LeetCode: 88. Merge Sorted Array (0) | 2021.10.09 |
LeetCode: 98. Validate Binary Search Tree (0) | 2021.10.09 |