| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 투포인터
- GenAI
- 슬라이딩윈도우
- sql코테
- tree
- graph
- 알고리즘
- nlp
- dfs
- SQL
- BFS
- two-pointer
- codeup
- Greedy
- 코드업
- 니트코드
- stratascratch
- Python
- 리트코드
- 생성형AI
- LeetCode
- Python3
- 릿코드
- 파이썬알고리즘
- 파이썬
- heap
- array
- GenerativeAI
- Stack
- binary Tree
Archives
- Today
- Total
Tech for good
[Leetcode/DFS, BFS, Graph] 1971. Find if Path Exists in Graph 본문
IT/Computer Science
[Leetcode/DFS, BFS, Graph] 1971. Find if Path Exists in Graph
Diana Kang 2025. 8. 8. 11:05

class Solution:
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
# 1. Make a graph
## To figure out neighbor using hash-map
## 0 => [1,2]
graph = defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u)
# 2. BFS from source
q = deque([source])
visited = set([source])
while q:
curr = q.popleft()
if curr == destination:
return True
for neighbor in graph[curr]:
if neighbor not in visited:
visited.add(neighbor)
q.append(neighbor)
return False'IT > Computer Science' 카테고리의 다른 글
| [Codepath/BFS, Graph] Find Path (0) | 2025.08.12 |
|---|---|
| [Leetcode/DFS, BFS, Graph, Matrix] 200. Number of Islands (0) | 2025.08.09 |
| [Leetcode/Array, Hash Table, Graph] 997. Find the Town Judge (0) | 2025.08.08 |
| [HackerRank] Inorder Traversal of Binary Tree (0) | 2025.08.04 |
| [Leetcode/Tree, Binary Tree, DFS] 572. Subtree of Another Tree (0) | 2025.07.26 |