-
Notifications
You must be signed in to change notification settings - Fork 0
/
all-nodes-distance-k-in-binary-tree.py
53 lines (49 loc) · 1.71 KB
/
all-nodes-distance-k-in-binary-tree.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
47
48
49
50
51
52
53
# 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]:
adj_list = defaultdict(list)
def dfs(node):
if not node:
return
if node.left:
adj_list[node.left.val].append(node.val)
adj_list[node.val].append(node.left.val)
left = dfs(node.left)
if node.right:
adj_list[node.right.val].append(node.val)
adj_list[node.val].append(node.right.val)
dfs(node.right)
# building adjacency list for the tree
node = root
dfs(node)
# for key in adj_list.keys():
# print(key, adj_list[key])
def bfs(queue, visited):
_k = 0
ans = [ ]
while queue:
_k += 1
size = len(queue)
for _ in range(size):
curr = queue.popleft()
for neighbor in adj_list[curr]:
if neighbor not in visited:
if _k == k:
print(neighbor)
ans.append(neighbor)
visited.add(neighbor)
queue.append(neighbor)
# print(visited)
return ans
if k == 0:
return [target.val]
queue = deque()
visited = set()
visited.add(target.val)
queue.append(target.val)
return bfs(queue, visited)