-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallNodesDistanceK.py
46 lines (37 loc) · 1.36 KB
/
allNodesDistanceK.py
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
queue = deque([(target.val, 0)]) # Queue[(node, distance_from_target)]
seen = set([target.val])
graph = self.construct_graph(root)
result = []
while queue:
curr, distance = queue.popleft()
if distance > k:
return result
if distance == k:
result.append(curr)
for neighbor in graph[curr]:
if neighbor not in seen:
queue.append((neighbor, distance + 1))
seen.add(neighbor)
return result
def construct_graph(self, node):
graph = defaultdict(list)
queue = deque([node])
while queue:
node = queue.popleft()
if node.left:
graph[node.val].append(node.left.val)
graph[node.left.val].append(node.val)
queue.append(node.left)
if node.right:
graph[node.val].append(node.right.val)
graph[node.right.val].append(node.val)
queue.append(node.right)
return graph