Given the root
of a binary tree, the value of a target node target
, and an integer k
, return an array of the values of all nodes that have a distance k
from the target node.
You can return the answer in any order.
For example:
Consider the following binary tree:
3
/ \
5 1
/ \ / \
6 2 0 8
/ \
7 4
If target
is node 5
and k
is 2
, the expected output should be [7, 4, 1]
.
Here's another example:
Given a binary tree with only one node:
1
If target
is node 1
and k
is 3
, the expected output should be []
because there are no nodes at distance 3 from node 1.
Constraints:
[1, 500]
.0 <= Node.val <= 500
Node.val
are unique.target
is the value of one of the nodes in the tree.0 <= k <= 1000
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
# Convert the binary tree to a graph
graph = defaultdict(list)
def build_graph(node):
if not node:
return
if node.left:
graph[node].append(node.left)
graph[node.left].append(node)
build_graph(node.left)
if node.right:
graph[node].append(node.right)
graph[node.right].append(node)
build_graph(node.right)
build_graph(root)
# BFS to find nodes at distance k from target
queue = deque([(target, 0)])
visited = {target}
result = []
while queue:
node, dist = queue.popleft()
if dist == k:
result.append(node.val)
elif dist < k:
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, dist + 1))
return result
Build the Graph:
Breadth-First Search (BFS):
target
node, a BFS is performed to find all nodes at a distance k
.target
node.visited
set is used to avoid cycles.target
is equal to k
, its value is added to the result
list.Consider the following binary tree:
3
/ \
5 1
/ \ / \
6 2 0 8
/ \
7 4
If target
is node 5
and k
is 2
, the nodes at distance 2
from node 5
are 7
, 4
, and 1
.
Time Complexity: O(N)
, where N
is the number of nodes in the binary tree. This is because:
O(N)
time.O(N)
time in the worst case (when we might visit all nodes).Space Complexity: O(N)
, where N
is the number of nodes in the binary tree. This is because:
N
nodes and N-1
edges.N
nodes in the worst case.N
nodes.Empty Tree:
None
, return an empty list.Target Not Found:
K = 0:
k
is 0
, the result should be a list containing only the value of the target node.K > Height of Tree:
k
is greater than the maximum possible distance from the target node, the result may be an empty list if there are no nodes at distance k
.from collections import defaultdict, deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
# Convert the binary tree to a graph
graph = defaultdict(list)
def build_graph(node):
if not node:
return
if node.left:
graph[node].append(node.left)
graph[node.left].append(node)
build_graph(node.left)
if node.right:
graph[node].append(node.right)
graph[node.right].append(node)
build_graph(node.right)
build_graph(root)
# BFS to find nodes at distance k from target
queue = deque([(target, 0)])
visited = {target}
result = []
while queue:
node, dist = queue.popleft()
if dist == k:
result.append(node.val)
elif dist < k:
for neighbor in graph[node]:
if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, dist + 1))
return result