Medium
All Nodes Distance K in Binary Tree — Python
Full explanation · Time O(n) · Space O(n)
# Time: O(n)
# Space: O(n)
import collections
class Solution(object):
def distanceK(self, root, target, K):
"""
:type root: TreeNode
:type target: TreeNode
:type K: int
:rtype: List[int]
"""
def dfs(parent, child, neighbors):
if not child:
return
if parent:
neighbors[parent.val].append(child.val)
neighbors[child.val].append(parent.val)
dfs(child, child.left, neighbors)
dfs(child, child.right, neighbors)
neighbors = collections.defaultdict(list)
dfs(None, root, neighbors)
bfs = [target.val]
lookup = set(bfs)
for _ in xrange(K):
bfs = [nei for node in bfs
for nei in neighbors[node]
if nei not in lookup]
lookup |= set(bfs)
return bfs