<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>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    unordered_map<TreeNode*, TreeNode*> parentMap;  // To store parent of each node

    // Function to find the parents of all nodes using BFS.
    void findParents(TreeNode* root) {
        if (root == NULL) return;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            TreeNode* node = q.front();
            q.pop();
            if (node->left != NULL) {
                parentMap[node->left] = node;  // Map the parent of the left child
                q.push(node->left);
            }
            if (node->right != NULL) {
                parentMap[node->right] = node;  // Map the parent of the right child
                q.push(node->right);
            }
        }
    }

    // Function to find all nodes at distance K from the target node.
    void findNodesAtDistanceK(TreeNode* target, int k, vector<int>& result) {
        queue<pair<TreeNode*, int>> q;
        unordered_set<TreeNode*> visited;
        q.push({target, 0});  // Push target node with initial distance 0
        visited.insert(target);

        while (!q.empty()) {
            TreeNode* node = q.front().first;
            int dist = q.front().second;
            q.pop();
            if(dist > k) continue;
            if (dist == k) {
                result.push_back(node->val);  // If the distance is k, add the node's value to result
            }

            // Continue BFS if distance is less than k
            if (dist < k) {
                if (node->left != NULL && visited.find(node->left) == visited.end()) {
                    q.push({node->left, dist + 1});
                    visited.insert(node->left);
                }
                if (node->right != NULL && visited.find(node->right) == visited.end()) {
                    q.push({node->right, dist + 1});
                    visited.insert(node->right);
                }
                if (parentMap.find(node) != parentMap.end() && visited.find(parentMap[node]) == visited.end()) {
                    q.push({parentMap[node], dist + 1});
                    visited.insert(parentMap[node]);
                }
            }
        }
    }

public:
    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
        vector<int> result;
        if (root == NULL || target == NULL) return result;

        // Find all parents of nodes.
        findParents(root);

        // Perform BFS from the target node to find all nodes at distance K.
        findNodesAtDistanceK(target, k, result);

        return result;
    }
};

15100