-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path863-all-nodes-distance-k-in-binary-tree.py
35 lines (33 loc) · 1.15 KB
/
863-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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from collections import deque
class Solution:
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
node_parent = {root: None}
queue = deque([root])
while queue:
node = queue.popleft()
for child in [node.left, node.right]:
if child:
queue.append(child)
node_parent[child] = node
res = []
queue = deque([(target, 0)])
visited = {target.val}
while queue:
node, distance = queue.popleft()
if distance == k:
res.append(node.val)
else:
for next_node in [node.left, node.right, node_parent[node]]:
if next_node and next_node.val not in visited:
queue.append((next_node, distance + 1))
visited.add(next_node.val)
return res
# time O(n)
# space O(n)
# using tree and bfs and build child_parent hashmap