Collect Binary-Tree Nodes at an Exact Distance from a Target
Quick Overview
Collect Binary-Tree Nodes at an Exact Distance from a Target. Given a binary-tree root, a pointer target to a node in that tree, and k, return the values of every node exactly k edges from the target.
Collect Binary-Tree Nodes at an Exact Distance from a Target
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Given a binary-tree root, a pointer `target` to a node in that tree, and `k`, return the values of every node exactly `k` edges from the target. Edges may be followed toward a parent or a child. Return the values in any order. The native interface uses a `TreeNode*` target; value notation in examples identifies that existing node.
```cpp
class Solution {
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int k);
};
```
### Examples
**Example 1:**
```text
Input: root = [8,3,10,1,6,9,12], target = node with value 3, k = 1
Output: [8,1,6]
Explanation: Its parent and two children are each one edge away. Any ordering is valid.
```
**Example 2:**
```text
Input: root = [4,2,7], target = node with value 4, k = 0
Output: [4]
Explanation: The target itself is the only node at distance zero.
```
### Constraints
- The tree contains `1` to `500` nodes.
- `0 <= Node.val <= 500`; all node values are unique.
- The target identifies a node in the tree.
- `0 <= k <= 1000`
### Contract considerations
Example arrays describe level-order tree shape with null placeholders; the callable receives the original pointers. Preserve the supplied TreeNode definition.
```hint Read the boundary condition
How can a search leave the target subtree without changing the tree?
```
### What a Strong Answer Covers
Example arrays describe level-order tree shape with null placeholders; the callable receives the original pointers. Preserve the supplied TreeNode definition.
### Follow-up Questions
Optional practice extension, separate from the original task: How would the search cost change when many distance queries share the same tree?
Overview: Collect Binary-Tree Nodes at an Exact Distance from a Target. Given a binary-tree root, a pointer target to a node in that tree, and k, return the values of every node exactly k edges from the target.
Collect Binary-Tree Nodes at an Exact Distance from a Target
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Given a binary-tree root, a pointer target to a node in that tree, and k, return the values of every node exactly k edges from the target. Edges may be followed toward a parent or a child. Return the values in any order. The native interface uses a TreeNode* target; value notation in examples identifies that existing node.
class Solution {
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int k);
};
Examples
Example 1:
Input: root = [8,3,10,1,6,9,12], target = node with value 3, k = 1
Output: [8,1,6]
Explanation: Its parent and two children are each one edge away. Any ordering is valid.
Example 2:
Input: root = [4,2,7], target = node with value 4, k = 0
Output: [4]
Explanation: The target itself is the only node at distance zero.
Constraints
The tree contains
1
to
500
nodes.
0 <= Node.val <= 500
; all node values are unique.
The target identifies a node in the tree.
0 <= k <= 1000
Contract considerations
Example arrays describe level-order tree shape with null placeholders; the callable receives the original pointers. Preserve the supplied TreeNode definition.
What a Strong Answer Covers Guidance
Example arrays describe level-order tree shape with null placeholders; the callable receives the original pointers. Preserve the supplied TreeNode definition.
Follow-up Questions Guidance
Optional practice extension, separate from the original task: How would the search cost change when many distance queries share the same tree?