forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshinsj4653.py
More file actions
50 lines (30 loc) · 892 Bytes
/
shinsj4653.py
File metadata and controls
50 lines (30 loc) · 892 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
[문제풀이]
# Inputs
# Outputs
# Constraints
# Ideas
[회고]
"""
# Definition for a Node.
class Node:
def __init__(self, val=0, neighbors=None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
from typing import Optional
from collections import deque
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
if not node:
return
clone = Node(node.val)
clones = {node: clone}
q = deque([node]) # 해당 라인 답지 참고
while q:
node = q.popleft()
for nei in node.neighbors:
if nei not in clones:
clones[nei] = Node(nei.val) # 답지 참고
q.append(nei)
clones[node].neighbors.append(clones[nei]) # 답지 참고
return clone