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.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2 Output: [7,4,1] Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
Example 2:
Input: root = [1], target = 1, k = 3 Output: []
Constraints:
[1, 500].0 <= Node.val <= 500Node.val are unique.target is the value of one of the nodes in the tree.0 <= k <= 1000In #863 All Nodes Distance K in Binary Tree, the goal is to find all nodes that are exactly K edges away from a given target node. Since binary tree nodes do not normally store references to their parents, a common strategy is to first convert the tree into an undirected structure.
One effective approach is to perform a DFS traversal to build a parent mapping using a HashMap. This allows movement both downward (to children) and upward (to parent). After building this map, a Breadth-First Search (BFS) starting from the target node explores neighbors level by level until distance K is reached. Nodes discovered at that level form the answer.
An alternative idea is a recursive DFS that calculates the distance from the target while propagating results up the tree. Both strategies ensure every node is visited at most once, leading to efficient performance.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| BFS with Parent Hash Map | O(N) | O(N) |
| DFS Distance Propagation | O(N) | O(N) |
take U forward
In this method, we first traverse the tree to establish a mapping of each node to its parent. This will allow us to easily move upwards in the tree. We then perform a BFS starting from the target node to explore all nodes at distance K. Using a queue to explore nodes level by level ensures we can track the distance from the target correctly.
Time Complexity: O(N), where N is the number of nodes, since each node is visited once. Space Complexity: O(N) for storing the parent map and the queue.
1from collections import deque, defaultdict
2
3class TreeNode:
4 def __init__(self, x):
5 self.val = x
6 self.
This Python solution establishes a map associating each node to its parent. Utilizing BFS from the target node, it systematically checks left, right, and parent nodes while avoiding revisiting by recording visited nodes.
In this method, we perform a DFS from the root to find the path to the target node while also tracking parents of each node. We then use this path to initiate DFS from the target node in all possible directions (left, right, up) to find nodes that are K distance away.
Time Complexity: O(N), traversing the tree requires visiting each node once. Space Complexity: O(N) for the parent map and the recursion call stack.
1function
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, DFS can be used by recursively computing the distance from the target node and propagating that information up the tree. When the recursion detects nodes at the required distance, they are added to the result list.
Yes, this problem or similar tree-distance variations have appeared in interviews at top tech companies. It tests understanding of tree traversal, graph conversion, and BFS/DFS problem-solving techniques.
A hash map is typically used to store parent references for each node. Combined with a queue for BFS and a visited set to prevent revisiting nodes, these structures make traversal efficient and manageable.
A common optimal approach is to build a parent pointer map using DFS and then run BFS from the target node. This allows traversal to both children and the parent, effectively treating the tree like an undirected graph. BFS stops once the distance reaches K.
This JavaScript implementation uses a recursion-based DFS to locate nodes at a specified distance from the target. Building a parent map supports upward traversal, while a Set caters to storing visited nodes, mitigating reprocessing.