<p>Given the <code>root</code> of a binary tree, the value of a target node <code>target</code>, and an integer <code>k</code>, return <em>an array of the values of all nodes that have a distance </em><code>k</code><em> from the target node.</em></p>

<p>You can return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png" style="width: 500px; height: 429px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
<strong>Output:</strong> [7,4,1]
Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1], target = 1, k = 3
<strong>Output:</strong> []
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 500]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 500</code></li>
	<li>All the values <code>Node.val</code> are <strong>unique</strong>.</li>
	<li><code>target</code> is the value of one of the nodes in the tree.</li>
	<li><code>0 &lt;= k &lt;= 1000</code></li>
</ul>

 class Solution {
public:
    // DFS function to map each node to its parent
    void dfs(TreeNode* root, TreeNode* par, map<TreeNode*, TreeNode*>& mp) {
        if (root == NULL) return;
        mp[root] = par;  // Store the parent of the current node
        if (root->left) {
            dfs(root->left, root, mp);  // Traverse the left subtree
        }
        if (root->right) {
            dfs(root->right, root, mp);  // Traverse the right subtree
        }   
    }

    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
        vector<int> ans;
        if (root == NULL) return ans;
        
        // Map to store parent pointers of all nodes
        map<TreeNode*, TreeNode*> mp;
        dfs(root, NULL, mp);  // Build the parent map using DFS

        // Queue for BFS, stores pairs of nodes and their distance from the target
        queue<pair<TreeNode*, int>> q;
        q.push({target, 0});  // Start BFS from the target node
        
        // Set to keep track of visited nodes
        set<TreeNode*> visited;
        visited.insert(target);  // Mark the target node as visited

        // BFS traversal
        while (!q.empty()) {
            auto it = q.front();
            TreeNode* node = it.first;
            int steps = it.second;
            q.pop();

            // If we've reached the required distance `k`, add node's value to the answer
            if (steps == k) ans.push_back(node->val);

            // Explore the left child if not visited
            if (node->left && visited.find(node->left) == visited.end()) {
                q.push({node->left, steps + 1});
                visited.insert(node->left);  // Mark it as visited
            }

            // Explore the right child if not visited
            if (node->right && visited.find(node->right) == visited.end()) {
                q.push({node->right, steps + 1});
                visited.insert(node->right);  // Mark it as visited
            }

            // Explore the parent node if not visited
            if (mp[node] != NULL && visited.find(mp[node]) == visited.end()) {
                q.push({mp[node], steps + 1});
                visited.insert(mp[node]);  // Mark it as visited
            }
        }

        return ans;
    }
};

15700